在 csproj 中标记程序集 CLSCompliant

Mark an assembly CLSCompliant in csproj

在这个 的回复中解释了如何在 csproj.

中设置 InternalsVisibleTo

我认为这也适用于 CLSCompliant:

<ItemGroup>
    <AssemblyAttribute Include="System.CLSCompliant">
      <_Parameter1>true</_Parameter1>
    </AssemblyAttribute>
</ItemGroup>

但不是! MSBuild 抱怨 true 无法从 string 转换为 bool:

> dotnet build
Microsoft (R) Build Engine version 16.4.0+e901037fe for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 418.35 ms for C:\Users\coder\OneDrive\Projects\pickall\src\PickAll.Sample\PickAll.Sample.csproj.
  Restore completed in 418.33 ms for C:\Users\coder\OneDrive\Projects\pickall\src\PickAll.Specs\PickAll.Specs.csproj.
  Restore completed in 418.33 ms for C:\Users\coder\OneDrive\Projects\pickall\src\PickAll\PickAll.csproj.
obj\Debug\netstandard2.0\PickAll.AssemblyInfo.cs(14,32): error CS1503: Argument 1: cannot convert from 'string' to 'bool' [C:\Users\coder\OneDrive\Projects\pickall\src\PickAll\PickAll.csproj]
obj\Debug\net461\PickAll.AssemblyInfo.cs(14,32): error CS1503: Argument 1: cannot convert from 'string' to 'bool' [C:\Users\coder\OneDrive\Projects\pickall\src\PickAll\PickAll.csproj]

Build FAILED.

obj\Debug\netstandard2.0\PickAll.AssemblyInfo.cs(14,32): error CS1503: Argument 1: cannot convert from 'string' to 'bool' [C:\Users\coder\OneDrive\Projects\pickall\src\PickAll\PickAll.csproj]
obj\Debug\net461\PickAll.AssemblyInfo.cs(14,32): error CS1503: Argument 1: cannot convert from 'string' to 'bool' [C:\Users\coder\OneDrive\Projects\pickall\src\PickAll\PickAll.csproj]
    0 Warning(s)
    2 Error(s)

Time Elapsed 00:00:18.37

有没有办法在 _Parameter1 标签内正确写入 布尔文字

appears that there is no way to do this 在 .csproj 文件中。在链接的 GitHub 问题的评论中多次提出并拒绝了多个解决方案。唯一的解决办法是添加一个空的 csharp 文件并在其中指定程序集属性。

例如,在 ArbitraryFileName.cs 中:

using System;
[assembly: CLSCompliant(true)]

您或许应该将此文件命名为 AssemblyInfo.cs,但实际名称是什么并不重要。

拉取请求 (Allow parameter type name to be specified for WriteCodeFragment task) 最近已合并到 MSBuild 存储库中,它可以完全按照您的原始问题中的描述在 csproj 文件中标记程序集 CLSCompliant。

在版本 16.10 预览版 3 中应该是 available soon(2021 年 5 月)(不确定是指 MSBuild 版本还是 Visual Studio 版本)。从提到的拉取请求评论中引用用户 Forgind:

This should be available in 16.10 preview 3. I will try to remember to ping you in this thread when that's available. That should be roughly a month from now.

解决方案 1

<ItemGroup>
    <AssemblyAttribute Include="System.CLSCompliant">
        <_Parameter1>true</_Parameter1>
        <_Parameter1_TypeName>System.Boolean</_Parameter1_TypeName>
    </AssemblyAttribute>
</ItemGroup>

解决方案 2

<ItemGroup>
    <AssemblyAttribute Include="System.CLSCompliant">
        <_Parameter1>true</_Parameter1>
        <_Parameter1_IsLiteral>true</_Parameter1_IsLiteral>
    </AssemblyAttribute>
</ItemGroup>

解决方案 3:

来自@cremore 的以下评论:
如果您写完整的 class 名称,那么 CLSCompliantAttribute,现在即使没有额外的 _Parameter1_TypeName_Parameter1_IsLiteral 标签也可以使用。

<ItemGroup>
    <AssemblyAttribute Include="System.CLSCompliantAttribute">
        <_Parameter1>true</_Parameter1>
    </AssemblyAttribute>
</ItemGroup>

重要提示:

  1. 我正在使用 Visual Studio 2022
  2. 如果这对您不起作用,请尝试更新Visual Studio(这是新添加的功能)