Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mark an assembly CLSCompliant in csproj

Tags:

c#

msbuild

In a reply of this question is explained how to set InternalsVisibleTo in csproj.

I presumed this worked also for CLSCompliant:

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

But is not! MSBuild complains that true can't be converted to from string to 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

Is there a way to correctly write a boolean literal inside _Parameter1 tag?

like image 751
gsscoder Avatar asked Dec 23 '22 19:12

gsscoder


2 Answers

Solution 1

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

Solution 2

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

Solution 3:

From a comment below by @cremore :
If you write the full class name, so CLSCompliantAttribute, this now works even without the additional _Parameter1_TypeName or _Parameter1_IsLiteral tags.

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

Important NOTES:

  1. I am using Visual Studio 2022
  2. If this does not work for you, try to update Visual Studio (this is newly added feature)
like image 175
Hakan Fıstık Avatar answered Jan 01 '23 17:01

Hakan Fıstık


A pull request (Allow parameter type name to be specified for WriteCodeFragment task) has recently been merged in the MSBuild repository that enables to mark an assembly CLSCompliant in the csproj file exactly as described in your original question.

It should be available soon (May 2021) in version 16.10 preview 3 (not sure if that refers to the MSBuild version or the Visual Studio version). Quoting user Forgind from the mentioned pull request comments:

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.

like image 29
0xced Avatar answered Jan 01 '23 16:01

0xced