20

The title says it all - how do I flag a .NET standard library as CLS-compliant?

I wrote a simple library in C# targeting .NET Standard 1.0 framework. It includes two enums:

public enum Alignments { Left, Center, Right }
public enum Actions { None, Total, Average, Count }

When I try to use the library in a .NET 4.6 project it flags the enums as non-CLS-compliant:

Warning CS3001 Argument type 'Actions' is not CLS-compliant

I cannot add anything to AssemblyInfo.cs since this is not used by .NET standard. It does not seem to be supported as a property in the .csproj file either.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
Quango
  • 12,338
  • 6
  • 48
  • 83
  • 3
    Can't you just place the attribute in any class of your project? – Oscar Feb 28 '18 at 09:57
  • 1
    Adding the attribute to the enum results in a new warning: `Warning CS3014 'Alignments' cannot be marked as CLS-compliant because the assembly does not have a CLSCompliant attribute` - go figure. So I dug through the documents and found the answer: you put the `[assembly: CLSCompliant(true)]` statement in regular code. Thanks for the help! – Quango Feb 28 '18 at 10:11
  • [CLSCompliant] stopped being useful a decade ago, discontinuation of JScript put the final nail in its coffin. Why did you add it to the assembly that uses this library? Never write code you don't understand, just remove it. – Hans Passant Feb 28 '18 at 11:02
  • @HansPassant I didn't put it in originally. Using the library without it then generates warnings in downstream code. It was a simple fix to remove it, so why not? – Quango Feb 28 '18 at 11:03
  • 2
    @HansPassant - Can you elaborate on how `CLSCompliant` is no longer useful (a link maybe)? Doesn't it still make sense to use it for compatibility with VB.NET, for example? – NightOwl888 Feb 28 '18 at 17:48
  • 1
    It mattered back in the early years when it was still possible to encounter languages that had trouble with certain types. Yes, like vb.net, until 2005. jscript until 2008. That's it, no more. Generics would be the bigger hangup in a wonky language, but that isn't checked by [CLSCompliant]. – Hans Passant Feb 28 '18 at 18:01
  • @HansPassant `CLSCompliant` does check for exposed unsigned integer types (`UInt32`, etc). Many popular languages today still don't support unsigned integers and those languages may find themselves in an interop scenario with .NET (such as Python, Java, ECMAScript, and OCaml). Given that those languages would probably interop with .NET through a marshal system instead of the CLR directly then I suppose it's reasonable to abandon the pretense of keeping a library project portable between languages. – Dai May 08 '19 at 00:36
  • @HansPassant I tried CLSCompliant recently and got a warning straight away ("Arrays as attribute arguments is not CLS-compliant"), so I still think it has some value. – dotnetnutty May 07 '21 at 14:18

1 Answers1

22

There isn't an AssemblyInfo.cs file, but assembly attributes can be added to any file in a .NET Standard project.

So adding this to a C# file in the project will make the assembly CLS-compliant:

using System; [assembly: CLSCompliant(true)]

Reference: NetStandard v1.0 docs

For convention I'll create an AssemblyInfo.cs file and place it there anyway.

Quango
  • 12,338
  • 6
  • 48
  • 83