17

A reply to this question is explained how to set InternalsVisibleTo in csproj.

I presumed this also work for CLSCompliant:

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

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

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
gsscoder
  • 3,088
  • 4
  • 33
  • 49
  • 1
    You've only shown half of what you've done - you haven't shown the C# code you've tried to use with the project file. – Jon Skeet Jan 27 '20 at 08:59
  • 1
    The project is: https://github.com/gsscoder/pickall. If I set the `CLSCompliant` via code, compilation succeed. I don't get warnings, if not from OmniSharp VSCode extension. It's not happy with code that references AngleSharp. I suppose because it's not marked `CLSCompliant(true)`. – gsscoder Jan 27 '20 at 10:11
  • 1
    A link to the project isn't nearly as helpful as a [mcve] *in the question*. (I'd also recommend temporarily putting your machine into English so that you can post English error messages.) – Jon Skeet Jan 27 '20 at 10:47
  • 1
    Hi, @JonSkeet. I've edited the question to post **MSBuild** output in **English**. (_To be honest I don't know why I didn't it before, I hate use UI in other languages_). I've also created a [gist](https://gist.github.com/gsscoder/e1d07aab53867faba6e46bec0801fca3) with the modified csproj. Rest of repository can be cloned from **GitHub**. If you need anything else, please let me know. Thank you for your time! – gsscoder Jan 27 '20 at 15:02
  • 1
    "Rest of repository can be cloned from GitHub." Right now, yes. Not necessarily in 10 years time, when this question may still exist. Or in 10 years it may look entirely different. And there's no doubt more code than is required to *just* demonstrate this problem. In order to make this question as useful as possible for both someone trying to answer, and someone reading the question to benefit from it later, please include a [mcve] *in the question*. – Jon Skeet Jan 27 '20 at 15:33
  • To reproduce it's really easy. Just clone the repo. Overwrite the project with the supplied gist and hit `dotnet build` from the terminal. I got the same behaviour using same version of .NET Core (**3.1.100**) on both **Windows 10** and **macOS Catalina**. I don't think the project in question is important. The **key** is: _does **MSBuild** can handle something different than string when defining attributes in csproj?_ I wasn't unable to find documentation about it. – gsscoder Jan 28 '20 at 07:03
  • Current versions of Visual Studio support this, but only if you write the full class name (`CLSCompliantAttribute`). – cremor Mar 29 '22 at 11:06
  • @gsscoder is there a possibility that you consider changing the accepted answer from the outdated answer to the most upvoted answer, as an approach has been provided recently to solve this problem exactly? thank you – Hakan Fıstık Apr 03 '23 at 12:40

3 Answers3

19

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,CLSCompliantAttribute, it 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)
  3. Source
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
  • 1
    Thanks Hakan, I think it deserves a test also with previous .NET versions – gsscoder Jan 31 '22 at 14:12
  • 1
    If you write the full class name, so `CLSCompliantAttribute`, this now works even without the additional `_Parameter1_TypeName` or `_Parameter1_IsLiteral` tags. – cremor Mar 29 '22 at 11:05
  • @cremor I test that and it worked, you are correct, Thank you for the note, I will update the answer – Hakan Fıstık Mar 29 '22 at 11:42
10

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.

0xced
  • 25,219
  • 10
  • 103
  • 255
2

It appears that there is no way to do this within the .csproj file.
Several solutions were proposed and rejected numerous times in the comments of the linked GitHub issue.
The only solution is to add an empty csharp file and specify the assembly attributes in there.

e.g., in ArbitraryFileName.cs:

using System;
[assembly: CLSCompliant(true)]

You should probably name this file AssemblyInfo.cs, but it really doesn't matter what the name actually is.

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Rand.Function
  • 770
  • 1
  • 7
  • 10