什么是“CLSCompliant”? .NET 中的属性?

发布于 2024-07-13 18:51:07 字数 139 浏览 6 评论 0原文

什么是 CLSCompliant 属性?

What is the CLSCompliant attribute?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

非要怀念 2024-07-20 18:51:07

当您想要确保类可以被任何其他 .NET 语言使用时,可以使用 CLSCompliant 属性标记类。
这些是基本规则:

  1. 无符号类型不应成为类的公共接口的一部分。 这意味着公共字段不应具有无符号类型,例如 uint 或 ulong ,公共方法不应返回无符号类型,传递给公共函数的参数不应具有无符号类型。 然而,无符号类型可以是私有成员的一部分。

  2. 诸如指针之类的不安全类型不应与public成员一起使用。 但是它们可以与 private 成员一起使用。

  3. 类名和成员名不应仅根据大小写而有所不同。 例如,我们不能有两个名为 MyMethodMYMETHOD 的方法。

  4. 只有属性和方法可以重载,运算符不应重载。

You mark classes with the CLSCompliant attribute when you want to make sure it can be used by any other .NET language.
These are the basic rules:

  1. 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 uint or ulong, 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.

  2. Unsafe types like pointers should not be used with public members. However they can be used with private members.

  3. Class names and member names should not differ only based on their case. For example we cannot have two methods named MyMethod and MYMETHOD.

  4. Only properties and methods may be overloaded, operators should not be overloaded.

極樂鬼 2024-07-20 18:51:07

其他答案都是正确的。 让我澄清一些事情——CLS 代表公共语言规范。 它是 .NET 语言必须实现和理解的最小规则集和所需的语言功能。 该集是通用类型系统的子集,它定义了类型的方式在 .NET 中定义。

符合 CLS 意味着您可以编写可由可在 CLR 上编译和运行的任何语言使用的代码。 但 CLS 合规性不是必需的,因此在很难或不可能实现 CLS 合规性的情况下,您可以灵活地进行操作。

如果您希望其他开发人员使用您的代码,您的 API(您的公共类和方法)应该符合 CLS。 您应该通过将 CLSCompliantAttribute 添加到程序集中来声明这一点。 如果您不是为其他人写作,则不需要遵守 CLS,尽管FxCop框架 警察)会不同意我的观点。

当你的程序集被标记为 CLSCompliantAttribute 时,编译器将(应该!)检查你的代码,看看在编译时它是否会违反任何 CLS 规则(ocdecio 提到了其中一些规则)并报告违规行为需要您修复。

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.

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.

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 (Framework Cop) would disagree with me.

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 ocdecio mentioned) and report violations to you for fixing.

纵情客 2024-07-20 18:51:07

它告诉其他代码使用者您的代码符合 CLS,并且让 C# 编译器检查它是否符合 CLS。

引用的文章包含有关 CLS 合规性要求的更多详细信息。

It tells other consumers of your code that it is CLS compliant, and also makes the C# compiler check that it's CLS compliant for you.

The referenced article contains a lot more detail about what CLS compliance entails.

眼波传意 2024-07-20 18:51:07

此处适合:要标记整个项目符合 CLS,请将此行添加到 AssemblyInfo.cs(可以在“解决方案资源管理器”的“属性”下找到)

[assembly:CLSCompliant(true)]

或 VB.NET 中的等效项 (AssemblyInfo.vb< /code> 隐藏在我的项目下)

<Assembly: CLSCompliant(True)>

感谢 使您的代码符合 CLS

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)

[assembly:CLSCompliant(true)]

or equivalently in VB.NET (AssemblyInfo.vb is hidden under My Project)

<Assembly: CLSCompliant(True)>

Thanks to Making Your Code CLS Compliant.

暮凉 2024-07-20 18:51:07

CLS 兼容是 CLR 允许的完整语言范围的子集。 它将其限制为大多数针对 CLR 的语言可能可用的子集。 这会增加但不能保证您的库可以被所有针对 CLR 的语言使用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文