Why is this name with an underscore not CLS Compliant?

前端 未结 8 1141
时光说笑
时光说笑 2020-12-01 11:45

Why do I get the compiler warning

Identifier \'Logic.DomainObjectBase._isNew\' is not CLS-compliant

for the following code?

8条回答
  •  爱一瞬间的悲伤
    2020-12-01 12:11

    CLS compliance has to do with interoperability between the different .NET languages. The property is not CLS compliant, because it starts with an underscore and is public (note: protected properties in a public class can be accessed from outside the assembly). Although this will work if the property is accessed from C# it may not if it is accessed from other .NET languages that don't allow underscores at the start of property names, hence it is not CLS-compliant.

    You are getting this compiler error, because somewhere in your code you have labelled your assembly as CLS compliant with a line something like this:

    [assembly: CLSCompliant(true)]
    

    Visual Studio includes this line in the AssemblyInfo.cs file which can be found under Properties in most projects.

    To get around this error you can either:

    1. Rename your property (recommended):

      protected bool isNew;
      
    2. Set your whole assembly to be non CLS compliant:

      [assembly: CLSCompliant(false)]
      
    3. Add an attribute just to your property:

      [CLSCompliant(false)]  
      protected bool _isNew;
      
    4. Change the scope of the property, so that it can not be seen outside the assembly.

      private bool _isNew;
      

提交回复
热议问题