如何在.NET Core 1.1中的程序集上使用自定义属性

MVi*_*nca 9 csproj .net-core

虽然我发现这篇关于在程序集上检索自定义属性的帖子,但我不确定如何在.NET Core 1.1中为程序集添加自定义属性.在.NET Framework中,我会做类似的事情:

[assembly: AdditionalLocation(@"..\ReadFromHere")]
Run Code Online (Sandbox Code Playgroud)

但我在Visual Studio中的netcore1.1项目没有AssemblyInfo.cs.我在哪里声明程序集的自定义属性?我可以在.csproj文件中放一些东西吗?

Mar*_*ich 18

您始终可以创建新AssemblyInfo.cs文件或任何其他.cs文件来执行相同操作.

但是,您也可以使用新的自动生成的程序集信息机制.您可以将其添加到csproj文件中,将替换Include属性值的值替换为自定义属性的类型名称:

<ItemGroup>
  <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
    <_Parameter1>DasMulli.Win32.ServiceUtils.Tests</_Parameter1>
  </AssemblyAttribute>
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)

  • 从来没有真正想过“AssemblyInfo.cs”没有什么特别之处,但既然你提到了它,那么我可以在任何地方放置 [assemble:...] 指令就完全有意义了。但出于这个原因,我觉得 .csproj 作为整个程序集范围内的东西的位置更合适。谢谢你的语法,这正是我所缺少的。 (6认同)
  • 仅供参考<AssemblyAttribute>策略仅适用于_string_值,因此您无法使用它来替换曾经存在于AssemblyInfo.cs中的非字符串属性(例如xunit的`[assembly:Xunit.CollectionBehavior(Xunit.CollectionBehavior.CollectionPerAssembly) )]`)不太可能支持非字符串:https://github.com/https://github.com/Microsoft/msbuild/issues/2281 (4认同)

fro*_*n10 5

当我将库从旧的 .net 框架移动到 .net 标准时,我遇到了这个问题,我设法通过添加曾经存在于 AssemblyInfo.cs 中的非字符串属性来解决该问题。这是我的 .csproj 文件的片段,当运行构建时,我的属性就在那里:

<ItemGroup>
  <AssemblyAttribute Include="Xunit.CollectionBehavior">
    <_Parameter1>Xunit.CollectionBehavior.CollectionPerAssembly</_Parameter1>
    <_Parameter1_IsLiteral>true</_Parameter1_IsLiteral>  
    <_Parameter1_TypeName>Xunit.CollectionBehavior.CollectionPerAssembly</_Parameter1_TypeName>
  </AssemblyAttribute>
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)

它已添加到 msbuild 中,如下所述: https: //github.com/dotnet/msbuild/issues/2281https://github.com/dotnet/msbuild/blob/main/documentation/Changelog.md#msbuild-16100这个版本。

希望能帮助到你!现在有了这个就好多了。