.NET 中的“CLSCompliant”属性是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/570452/
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
What is the 'CLSCompliant' attribute in .NET?
提问by Praveen Sharma
What is the CLSCompliantattribute?
CLSCompliant属性是什么?
回答by Otávio Décio
You mark classes with the CLSCompliantattribute when you want to make sure it can be used by any other .NET language.
These are the basic rules:
CLSCompliant当您想确保它可以被任何其他 .NET 语言使用时,您可以使用该属性标记类。
这些是基本规则:
Unsigned types should not be part of the public interface of the class. What this means is public fields should not have unsigned types like
uintorulong, public methods should not return unsigned types, parameters passed to public function should not have unsigned types. However unsigned types can be part of private members.Unsafe types like pointersshould not be used with
publicmembers. However they can be used withprivatemembers.Class names and member names should not differ only based on their case. For example we cannot have two methods named
MyMethodandMYMETHOD.Only properties and methods may be overloaded, operators should not be overloaded.
无符号类型不应该是类的公共接口的一部分。这意味着公共字段不应具有无符号类型,例如
uintorulong,公共方法不应返回无符号类型,传递给公共函数的参数不应具有无符号类型。然而,无符号类型可以是私有成员的一部分。像指针这样的不安全类型不应与
public成员一起使用。但是,它们可以与private成员一起使用。类名和成员名不应仅根据大小写而不同。例如,我们不能有两个名为
MyMethod和 的方法MYMETHOD。只有属性和方法可以重载,运算符不应重载。
回答by Jon Skeet
It tells other consumers of your code that it is CLS compliant, and also makes the C# compiler checkthat it's CLS compliant for you.
它告诉您的代码的其他使用者它符合 CLS,并且还使 C# 编译器检查它是否符合 CLS。
The referenced article contains a lot more detail about what CLS compliance entails.
引用的文章包含有关 CLS 合规性要求的更多详细信息。
回答by Jon Skeet
The other answers are correct. Let me clarify some things--CLS stands for the Common Language Specification. It's the minimal set of rules and required language features that a .NET language must implement and understand. This set is a subset of the common type system, which defines how types are defined in .NET.
其他答案都是正确的。让我澄清一些事情——CLS 代表公共语言规范。这是 .NET 语言必须实现和理解的最小规则集和所需的语言功能。这个集合是公共类型系统的一个子集,它定义了在 .NET 中如何定义类型。
Being CLS compliant means that you can write code that can be consumed by any language that can be compiled and run on the CLR. But CLS compliance is not required, giving you the flexibility in cases where CLS compliance would be hard or impossible to do.
符合 CLS 意味着您可以编写可由任何可在 CLR 上编译和运行的语言使用的代码。但是 CLS 合规性不是必需的,在 CLS 合规性很难或不可能做到的情况下为您提供灵活性。
If you intend your code to be consumed by other developers, your API (your public classes and methods) should be CLS compliant. You should declare this by adding the CLSCompliantAttribute to your assemblies. If you are not writing for others, CLS compliance is not necessary, although FxCop(FrameworkCop) would disagree with me.
如果您打算让其他开发人员使用您的代码,则您的 API(您的公共类和方法)应该符合 CLS。您应该通过向程序集添加 CLSCompliantAttribute 来声明这一点。如果您不是为他人写作,则不需要遵守 CLS,尽管FxCop(FrameworkCop)会不同意我的观点。
When your assembly is marked with the CLSCompliantAttribute, the compiler will (should!) check your code to see if, when compiled, it will violate any of the CLS rules (some of which ocdeciomentioned) and report violations to you for fixing.
当您的程序集标有 CLSCompliantAttribute 时,编译器将(应该!)检查您的代码,看看在编译时它是否会违反任何 CLS 规则(其中一些ocdecio提到)并向您报告违规情况以进行修复。
回答by JaredPar
CLS compliant is a subset of the full language spectrum allowed by the CLR. It restricts it to subsets that are likely available by the majority of languages that target the CLR. This increases, but does not guarantee, that your library can be used by all languages targeting the CLR.
CLS 兼容是 CLR 允许的完整语言范围的一个子集。它将它限制为大多数面向 CLR 的语言可能可用的子集。这会增加但不保证您的库可以被所有面向 CLR 的语言使用。
回答by Markus Hartmair
As it fits here: To mark a whole project CLS compliant add this line to AssemblyInfo.cs(can be found under Properties in Solution Explorer)
适合此处:要标记整个项目符合 CLS,请将这一行添加到AssemblyInfo.cs(可以在解决方案资源管理器的“属性”下找到)
[assembly:CLSCompliant(true)]
or equivalently in VB.NET (AssemblyInfo.vbis hidden under My Project)
或等效地在 VB.NET 中(AssemblyInfo.vb隐藏在我的项目下)
<Assembly: CLSCompliant(True)>
Thanks to Making Your Code CLS Compliant.
感谢让您的代码符合 CLS。

