1

次のようにPowerShellにDLLをロードするように起動アクションが設定されているVisualStudioプロジェクトがあります。

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup Condition="'$(Platform)' == 'Debug|x86'">
    <StartAction>Program</StartAction>
    <StartProgram>C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe</StartProgram>
    <StartArguments>-NoProfile -NoExit -Command Add-Type -Path  "Foo.dll" -PassThru | Select FullName</StartArguments>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
    <StartAction>Program</StartAction>
    <StartProgram>c:\windows\system32\windowspowershell\v1.0\powershell.exe</StartProgram>
    <StartArguments>-NoProfile -NoExit -Command Add-Type -Path  "Foo.dll" -PassThru | Select FullName</StartArguments>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
    <StartProgram>C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe</StartProgram>
    <StartArguments>-NoProfile -NoExit -Command Add-Type -Path  "Foo.dll" -PassThru | Select FullName</StartArguments>
    <StartAction>Program</StartAction>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
    <StartProgram>c:\windows\system32\windowspowershell\v1.0\powershell.exe</StartProgram>
    <StartArguments>-NoProfile -NoExit -Command Add-Type -Path  "Foo.dll" -PassThru | Select FullName</StartArguments>
    <StartAction>Program</StartAction>
  </PropertyGroup>
</Project>

冗長性を取り除くために、ファイルを次のように手作業で編集しました。

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup '$(Platform)' == 'x86'">
    <StartProgram>C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe</StartProgram>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Platform)' == 'AnyCPU' or Condition="'$(Platform)' == 'x64'">
    <StartProgram>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</StartProgram>
  </PropertyGroup>
  <PropertyGroup>
    <StartAction>Program</StartAction>
    <StartArguments>-NoProfile -NoExit -Command Add-Type -Path  "Foo.dll" -PassThru | Select FullName</StartArguments>
  </PropertyGroup>
</Project>

ここで、ハードコードされた「Foo.dll」を「$(TargetPath)」のようなものに置き換えたいと思います。ただし、それは空の文字列に評価されます。以下も空の文字列に解決されます。

  • $(AssemblyName)
  • $(TargetName)$(TargetExt)
  • $(TargetFileName)

値を設定<StartArguments>-NoProfile -NoExit -Command Add-Type -Path "$(Platform)" -PassThru | Select FullName</StartArguments>すると、コマンドラインが作成されます"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -NoExit -Command Add-Type -Path "x64" -PassThru | Select FullName

4

1 に答える 1

2

次のようなすべてのインポートディレクティブの後にプロパティ宣言を移動します<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

$(TargetPath) は最初は Microsoft.Common.targets で定義されているため、そのファイルがスクリプトにインポートされるまで値は含まれません。インポート前 (およびインポートされたプロパティが表示される前) にプロパティが定義されている場合、トップダウン プロパティ評価フェーズ中に、未定義のプロパティは空の文字列に解決されます。

注: 私は誤って、元の .proj ファイルではなく、 proj* .user * ファイルを意味していることに気付きませんでした。Visual Studio がいつ .user ファイルをロードし、ロード中にどのコンテキストが設定されたかはわかりません。元の .targets ファイルの定義を使用してみることができます。これは、Microsoft.common.targets で TargetDir がどのように定義されているかです: <TargetDir Condition="'$(OutDir)' != ''">$([MSBuild]::Escape($([System.IO.Path]::GetFullPath($([System.IO.Path]::Combine( $(MSBuildProjectDirectory), $(OutDir)))))))</TargetDir>

以前のビルド コンテキストなしで .user ファイルがロードされた場合でも、$(OutDir) は適切に解決されません。したがって、これはまだあなたの質問に対する不完全な回答です =(

于 2012-10-08T06:46:09.120 に答える