为什么 C# 包含不符合 CLS 的编程结构?

发布于 2024-10-17 18:10:51 字数 327 浏览 4 评论 0原文

.NET 旗舰语言包含不符合 CLS 的编程结构,这似乎很奇怪。这是为什么?

示例(来自此处):两个或更多仅区分大小写定义的公共/受保护/受保护内部成员

public int intA = 0;
public int INTA = 2; 

public int x = 0;

public void X()
{
} 

It seems strange that the flagship language of .NET would include programming constructs that are not CLS-compliant. Why is that?

Example (from here): Two or more public / protected / protected internal members defined with only case difference

public int intA = 0;
public int INTA = 2; 

or

public int x = 0;

public void X()
{
} 

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

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

发布评论

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

评论(7

旧瑾黎汐 2024-10-24 18:10:51

即使无符号整数也不兼容(至少在公共 API 上),但它们对于进行位移的人来说非常重要,特别是在右移时(有符号和无符号具有不同的右移行为)。

它们最终是为了让您在适当的时候可以自由地使用它们。我可以不太热衷于区分大小写 - 尽管拥有字段和财产的便利性仅因大小写而异,但我认为我可以幸福地被迫使用不同的名称!特别是现在我们已经自动实现了属性...

这类似于它让您使用 unsafe 的方式 - 此处的区别在于一些无符号整数不会破坏整个运行时的稳定性,因此它们不会不需要那么强大的莫莉守卫。

如果你愿意,你可以添加:

[assembly: CLSCompliant(true)]

,编译器会在你出错时告诉你。

最后:大多数代码(按数量)不会作为组件使用。它是为了完成一项工作而编写的,并且可能被内部的其他代码使用。主要是库编写者/供应商需要担心 CLS 合规性等问题。 (从数字上看)那是少数。

Even unsigned integers aren't compliant (at least, on the public API), yet they are very important to people who do bit-shifting, in particular when right-shifting (signed and unsigned have different right-shift behaviours).

They are ultimately there to give you the freedom to use them when appropriate. The case-sensitivity one I could get less religious about - despite the convenience of having a field and property differ only by case, I think I could live happily with being forced to use a different name! Especially now we have automatically implemented properties...

This is similar to how it lets you use unsafe - the difference here being that a few unsigned integers aren't going to destabilise the entire runtime, so they don't need quite as strong molly-guards.

You can add:

[assembly: CLSCompliant(true)]

if you like, and the compiler will tell you when you get it wrong.

And finally: most code (by volume) is not consumed as a component. It is written to do a job, and maybe consumed by other code in-house. It is mainly library writers / vendors that need to worry about things like CLS compliance. That is (by the numbers) the minority.

丢了幸福的猪 2024-10-24 18:10:51

这不是 CLS 合规性的运作方式。这是的负担。 C# 本身并没有严格遵守,这会使其成为一种表达能力很差的语言。将所有 .NET 语言降低到最低公分母将会很快扼杀该平台作为可行的编程环境的能力。

您需要确保程序集中的公开可见类型符合 CLS 合规性。确保类成员不只是大小写不同是非常简单的。让编译器通过使用 [ assembly:CLSCompliant(true)] 属性来帮助您,当您出现失误时,编译器会警告您。

That's not how CLS compliance works. It is something that's your burden. C# doesn't restrain itself to strict compliancy itself, that would make it a language with poor expressivity. Dragging all .NET languages down to the lowest common denominator would have quickly killed the platform as a viable programming environment.

It is up to you to ensure that the publicly visible types in your assembly meet CLS compliancy. Making sure that class members don't differ only by case is very simple to do. Let the compiler help you out by using the [assembly:CLSCompliant(true)] attribute and the compiler will warn you when you slipped.

带上头具痛哭 2024-10-24 18:10:51

请参阅 http://msdn.microsoft.com/en-us/library/bhc3fa7f。 .aspx.

CLS 是一种人们可以选择加入的规范。引述上面的话:

当您设计自己的符合 CLS 的组件时,使用符合 CLS 的工具会很有帮助。在没有此支持的情况下编写符合 CLS 的组件会更加困难,因为否则您可能无法访问想要使用的所有 CLS 功能。

某些符合 CLS 的语言编译器(例如 C# 或 Visual Basic 编译器)使您能够指定您希望代码符合 CLS。这些编译器可以检查 CLS 合规性,并在您的代码使用 CLS 不支持的功能时让您知道。 C# 和 Visual Basic 编译器允许您将程序元素标记为符合 CLS,如果代码不符合 CLS,这将导致编译器生成编译时错误。例如,以下代码会生成编译器警告。


上述链接中的示例代码:

using System;

// Assembly marked as compliant.
[assembly: CLSCompliant(true)]

// Class marked as compliant.
[CLSCompliant(true)]
public class MyCompliantClass {
   // ChangeValue exposes UInt32, which is not in CLS.
   // A compile-time warning results.
   public void ChangeValue(UInt32 value){ }

   public static void Main( ) {
   int i = 2;
   Console.WriteLine(i);
   }
}

此代码生成以下 C# 警告:

复制警告 CS3001:参数类型“uint”不符合 CLS

See http://msdn.microsoft.com/en-us/library/bhc3fa7f.aspx.

CLS is a specification that one opts-in to. Quote from above:

When you design your own CLS-compliant components, it is helpful to use a CLS-compliant tool. Writing CLS-compliant components without this support is more difficult because otherwise you might not have access to all the CLS features you want to use.

Some CLS-compliant language compilers, such as the C# or Visual Basic compilers, enable you to specify that you intend your code to be CLS-compliant. These compilers can check for CLS compliance and let you know when your code uses functionality that is not supported by the CLS. The C# and Visual Basic compilers allow you to mark a program element as CLS-compliant, which will cause the compiler to generate a compile-time error if the code is not CLS-compliant. For example, the following code generates a compiler warning.


Example code from above link:

using System;

// Assembly marked as compliant.
[assembly: CLSCompliant(true)]

// Class marked as compliant.
[CLSCompliant(true)]
public class MyCompliantClass {
   // ChangeValue exposes UInt32, which is not in CLS.
   // A compile-time warning results.
   public void ChangeValue(UInt32 value){ }

   public static void Main( ) {
   int i = 2;
   Console.WriteLine(i);
   }
}

This code generates the following C# warning:

Copy warning CS3001: Argument type 'uint' is not CLS-compliant

乙白 2024-10-24 18:10:51

我对 CLS 合规性的两点看法

.net 语言都是其创建时已存在的语言的演变。这些语言经过精心设计,以便您可以轻松地将基础项目转换为 .Net 项目,而无需太多的开发工作。由于语言之间存在巨大差异,因此需要某种约定才能使语言之间进行交流。以下面的语言为例:

VB.Net 是一种源自早期语言 VB6 的语言。它的风格应该与 VB6 非常相似,因此采用了 VB6 所使用的很多约定。由于 VB6 被认为易于非开发人员学习/使用,因此它具有某些使其更适合白痴的特性。动态类型、不区分大小写是其中两个。

C#.Net/C++.Net 是对程序员更友好的 C++ 的衍生版本。由于它们是这种语言的演变,因此它具有 C++ 可以让您做的事情。区分大小写、静态类型等。

现在,当面对两种不同的语言时,他们希望实现互操作,微软做了唯一合理的事情。他们通过使用基本上是软件合约来限制两种语言如何相互交互。由于语言差异,此代码只能以这种方式使用。

例如调用 C# 代码的 VB.Net 代码
如果 C# 代码有两个仅大小写不同的函数,X() 与 x(),VB.net 将永远无法正确调用此代码,因为它不区分大小写。 CLS 合规性必须将其定为非法。如果您查看其他规则,它们基本上对不同语言之间的其他语言功能执行相同的操作。

My two cents about CLS Compliance

The .net languages are all evolutions of languages that were in existence at the time it was created. The languages were crafted so that you could easily convert the base projects into .Net projects without too much development effort. Due to the vast differences between the languages there needed to be some sort of convention for the languages to talk with each other. Take the language examples below:

VB.Net is a language that is derived from the earlier language VB6. It's suppose to be very similar in style to VB6 and as such takes alot of the conventions VB6 used. Since VB6 was suppose to be easy to learn/use by non developers it has certain characteristics that make it more idiot proof. Dynamic typing, case insensitivity being two of these things.

C#.Net/C++.Net are derivatives of the more programmer friendly C++. Since they're an evolution of this language it has things in it that C++ would let you do. Case sensitivity, static typing etc.

Now when faced with two dissimilar languages that they wanted to make interoperable Microsoft did the only reasonable thing. They made restrictions on how the two languages can interact with each other through the use of the basically a software contract. This code can be used in only this way because of the differences in the languages.

For example take VB.Net code calling C# code
If the C# code had two functions that differed only in case, X() vs x(), VB.net would never be able to call this code correctly since it is case insensitive. The CLS compliance has to make this illegal. If you look at the other rules they're basically doing the same thing for other language features between the different languages.

丶情人眼里出诗心の 2024-10-24 18:10:51

我猜想大小写不敏感仅包含在 CLS 合规性中,以便 VB.NET 可以符合 CLS 合规性。据我了解,如果特定的语言构造不符合 CLS,则不会有问题,除非您使用它的方式使得不兼容的部分可以在代码的公共 API 中使用。

Microsoft 的暗示似乎是,CLS 合规性仅在您从不同语言访问的代码中很重要(例如从 VB.NET 项目引用 C# 程序集)。

I would guess the case insensitivity was only included in CLS compliance so that VB.NET could be CLS compliant. From what I understand, there is no issue if a particular language construct is not CLS compliant unless you are using it in such a way that the incompliant peices are available in the public API of your code.

The hint from Microsoft would seem to be that CLS compliance is only important in code that you are accessing from different languages (such as referencing a C# assembly from a VB.NET project).

送你一个梦 2024-10-24 18:10:51

我认为微软想给开发者自由。如果没有必要的话,没有限制。 C# 不受 CLS 限制,因为并非每个人都需要与 VB 互操作。

I think Microsoft wanted to give developers freedom. No constraints if not necessary. C# is not restricted by CLS because not everyone needs interoperability with VB.

匿名的好友 2024-10-24 18:10:51

如果对于编程语言中应包含哪些功能达成了普遍共识,那么世界将只需要一种编程语言(它恰好包含每个人都同意应该存在的那些功能)。当然,现实中有些人会认为这是其他人不关心(甚至觉得讨厌)的重要功能。 CLS 标准本质上做了三件事:

  1. 它表示某些功能非常重要,任何不包含这些功能的语言都应被视为不适合通用 .net 编程。
  2. 它表示,其库不需要除列出的功能之外的任何功能的程序员应该期望使用任何适合通用 .net 编程的语言的程序员都可以使用他们的库。
  3. 它告诉程序员,如果他们的库的某些部分需要使用语言不需要支持的功能,那么这些部分可能无法在适合 .net 编程的语言中使用,但不包含所需的功能。

当 vb.net 或 C# 等语言允许创建不符合 CLS 的编程时,这意味着 Microsoft 认为某些功能足够有用,足以证明包含在这些语言中,但还没有那么精彩或没有争议,足以证明强制要求所有语言都是合理的包括他们。

If there was universal agreement about what features should be included in a programming language, the world would only need one programming language (which would include precisely those features everyone agreed should be there). Of course, in reality some people will regard as important features which others don't care about (or even find distasteful). The CLS standard essentially does three things:

  1. It says that certain features are so important that any language which does not include them should be considered inadequate for general-purpose .net programming.
  2. It says that programmers whose libraries don't require any features other than those listed should expect that their libraries will be usable by programmers using any language that is suitable for general-purpose .net programming.
  3. It informs programmers that if parts of their libraries would require the use of features which languages are not required to support, those parts might not be usable in languages which are suitable for .net programming, but don't include the required features.

When languages like vb.net or C# allow the creation of non-CLS compliant programming, what that means is that Microsoft decided that certain features were useful enough to justify inclusion in those languages, but not so wonderful or noncontroversial as to justify mandating all languages include them.

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