我想在編譯主單元之前在程序集中嵌入本地引用。但是寫的目標不起作用。 <Target Name="EmbedLocal" BeforeTargets="CoreCompile"> <Message Text="Run EmbedLocal for $(MSBuildProjectFullPath)..." Importance="high"/> <ItemGroup> <EmbeddedResource Include="@( ReferencePath->WithMetadataValue( 'CopyLocal', 'true' )->Metadata( 'FullPath' ) )"/> </ItemGroup> <Message Text="Embed local references complete for $(OutputPath)$(TargetFileName)." Importance="high" /> </Target>@(EmbeddedResource) 此時包含有效的路徑列表。更新:現在我的導入文件包含:<Project ToolsVersion="$(MSBuildToolsVersion)" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <EmbedLocalReferences Condition=" '$(EmbedLocalReferences)' == '' ">True</EmbedLocalReferences> </PropertyGroup> <Target Name="EmbedLocal" BeforeTargets="ResolveReferences" Condition=" '$(EmbedLocalReferences)' == 'True' "> <Message Text="Run EmbedLocal for $(MSBuildProjectFullPath)..." Importance="high"/> <ItemGroup> <EmbeddedResource Include="@(ReferenceCopyLocalPaths->WithMetadataValue( 'Extension', '.dll' )->Metadata( 'FullPath' ))"> <LogicalName>%(ReferenceCopyLocalPaths.Filename)%(ReferenceCopyLocalPaths.Extension)</LogicalName> </EmbeddedResource> </ItemGroup> <Message Text="Embed local references complete for $(OutputPath)$(TargetFileName)." Importance="high" /> </Target></Project>它工作正常。輸出程序集包含所有 .dll 引用作為 EmbeddedResource。
1 回答

慕村225694
TA貢獻1880條經驗 獲得超4個贊
MSBuild。在構建之前創建 EmbeddedResource
您可以嘗試對csproj 文件使用BeforeBuild操作以包含嵌入的資源:
<Target Name="BeforeBuild">
...
<ItemGroup>
<EmbeddedResource Include="..."/>
</ItemGroup>
...
</Target>
現在 MSBuild 會將此文件作為嵌入資源添加到您的程序集中。
更新:
謝謝@Martin Ullrich。他指出了正確的方向,我們可以用<Target Name="EmbedLocal" BeforeTargets="PrepareForBuild">在Directory.Build.props來解決這個問題。您可以檢查它是否適合您。
<Target Name="EmbedLocal" BeforeTargets="PrepareForBuild">
...
<ItemGroup>
<EmbeddedResource Include="..."/>
</ItemGroup>
...
</Target>
- 1 回答
- 0 關注
- 394 瀏覽
添加回答
舉報
0/150
提交
取消