Disable CLS compliance checking in C#
Asked Answered
R

2

9

I'm working on code that have the following attributes on some of its methods:

[CLSCompliantAttribute(false)] 

How is it that when I build the code as is, I see that the compliance checking is being performed, and when I comment it out, it seems that the compliance checking is NOT being performed?

I've expected the opposite behavior...

Ramsdell answered 1/11, 2010 at 17:26 Comment(1)
Can you post your warning message?Dustpan
L
10

Adding [CLSCompliant(false)] marks the member you add it to as non-compliant.

If you mark the member as non-compliant, the compiler will not warn you if it isn't compliant. (Since you already said that it's not compliant.)

If, however, the member is marked as compliant (either explicitly or indirectly from an assembly-level attribute), but it is in fact not compliant (for example, it takes a uint), the compiler will warn you (since the attribute is now lying about the member).

Ladder answered 1/11, 2010 at 17:29 Comment(4)
so, if I get the warning - does it mean that I have the attribute : [CLSCompliant(true)] somewhere in my code in a higher scope?Ramsdell
You probably have it at the assembly level. Look for [assembly:CLSCompliant(true)] in Properties/AssemblyInfo.csGambier
For our VB.Net friends use this <CLSCompliant(False)>Geniegenii
This information is also inside SharedAssemblyInfo in many solutions - where we re-use some common assembly attributesBob
R
4

You can add it to AssemblyInfo.cs for example, and group all assembly:*. Like :

using System;
using System.Reflection;
using System.Runtime.InteropServices;
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCulture("")]
[assembly: CLSCompliant(false)]


// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is     exposed to COM
[assembly: Guid("d29c53b6-88e4-4b33-bb86-f39b4c733542")]

// Version information for an assembly consists of the following four     values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Revision and Build     Numbers 
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Rosaleerosaleen answered 28/3, 2017 at 12:51 Comment(1)
I had to add a using here: using System; Trivial, but worth mentioning.Bob

© 2022 - 2024 — McMap. All rights reserved.