gpt4 book ai didi

c# - 建立。在构建之前创建 EmbeddedResource

转载 作者:太空宇宙 更新时间:2023-11-03 22:42:18 31 4
gpt4 key购买 nike

我想在编译主单元之前在程序集中嵌入本地引用。但书面目标不起作用。

  <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。

最佳答案

MSBuild. Create EmbeddedResource before build

您可以尝试对 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>

关于c# - 建立。在构建之前创建 EmbeddedResource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51607558/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com