C# 为什么这个带有下划线的名称不符合 CLS?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1195030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is this name with an underscore not CLS Compliant?
提问by MatthewMartin
Why do I get the compiler warning
为什么我会收到编译器警告
Identifier 'Logic.DomainObjectBase._isNew' is not CLS-compliant
标识符“Logic.DomainObjectBase._isNew”不符合 CLS
for the following code?
对于以下代码?
public abstract class DomainObjectBase
{
protected bool _isNew;
}
采纳答案by Reed Copsey
From the Common Language Specification:
来自公共语言规范:
CLS-compliant language compilers must follow the rules of Annex 7 of Technical Report 15 of the Unicode Standard 3.0, which governs the set of characters that can start and be included in identifiers. This standard is available from the Web site of the Unicode Consortium.
符合 CLS 的语言编译器必须遵循 Unicode 标准 3.0 的技术报告 15 的附件 7 的规则,该规则管理可以开始和包含在标识符中的字符集。该标准可从 Unicode Consortium 的 Web 站点获得。
If you look this up:
如果你看这个:
That is, the first character of an identifier can be an uppercase letter, lowercase letter, titlecase letter, modifier letter, other letter, or letter number. The subsequent characters of an identifier can be any of those, plus non-spacing marks, spacing combining marks, decimal numbers, connector punctuations, and formatting codes (such as right-left-mark). Normally the formatting codes should be filtered out before storing or comparing identifiers.
也就是说,标识符的第一个字符可以是大写字母、小写字母、标题字母、修饰字母、其他字母或字母数字。标识符的后续字符可以是这些字符中的任何一个,加上非空格标记、空格组合标记、十进制数字、连接符标点符号和格式代码(例如左右标记)。通常在存储或比较标识符之前应该过滤掉格式代码。
Basically, you can't start an identifier with an underscore - this violates CLS compliant on a visible (public/protected) field.
基本上,您不能以下划线开头标识符 - 这违反了可见(公共/受保护)字段上的 CLS 兼容。
回答by jason
The leading underscoreconcomitant with _isNew
being visible (i.e., not private).
前导下划线伴随_isNew
可见(即,非私有)。
回答by TcKs
Because the name of the data member, _isNew
, start's with an underscore.
因为数据成员的名称_isNew
以下划线开头。
回答by Lloyd
A CLS-compliant identifier should not start with an underscore.
符合 CLS 的标识符不应以下划线开头。
回答by NotMe
The underscore causes the problem. Common practice is that the underscore is reserved for private fields. protected / public members should be properly cased and named.
下划线导致问题。通常的做法是下划线是为私有字段保留的。受保护/公共成员应正确区分大小写和命名。
For example:
例如:
public abstract class DomainObjectBase{
private bool _isNew;
protected bool IsNew { get { return _isNew; } set { _isNew = value;} }
}
OR, if you want to use 3.x and get rid of the private field:
或者,如果您想使用 3.x 并摆脱私有字段:
public abstract class DomainObjectBase{
protected bool IsNew { get; set; }
}
回答by Frozenskys
回答by Martin Brown
CLS compliancehas to do with interoperability between the different .NETlanguages. 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.
CLS 合规性与不同.NET语言之间的互操作性有关。该属性不符合 CLS,因为它以下划线开头并且是公共的(注意:公共类中的受保护属性可以从程序集外部访问)。虽然如果从 C# 访问属性,这将起作用,但如果从不允许在属性名称开头使用下划线的其他 .NET 语言访问,则可能不起作用,因此它不符合 CLS。
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:
您收到此编译器错误,因为您在代码中的某处将程序集标记为符合 CLS 的行,如下所示:
[assembly: CLSCompliant(true)]
Visual Studio includes this line in the AssemblyInfo.cs file which can be found under Properties in most projects.
Visual Studio 在 AssemblyInfo.cs 文件中包含这一行,该文件可在大多数项目的 Properties 下找到。
To get around this error you can either:
要解决此错误,您可以:
Rename your property (recommended):
protected bool isNew;
Set your whole assembly to be non CLS compliant:
[assembly: CLSCompliant(false)]
Add an attribute just to your property:
[CLSCompliant(false)] protected bool _isNew;
Change the scope of the property, so that it can not be seen outside the assembly.
private bool _isNew;
重命名您的财产(推荐):
protected bool isNew;
将整个程序集设置为不符合 CLS:
[assembly: CLSCompliant(false)]
只为您的属性添加一个属性:
[CLSCompliant(false)] protected bool _isNew;
更改属性的范围,使其在程序集之外无法看到。
private bool _isNew;