将项目中的所有类标记为符合 CLS

发布于 2024-08-12 02:49:10 字数 176 浏览 7 评论 0原文

如果我有一个项目,并将其标记为

<Assembly: CLSCompliant(True)> 

是否需要将 CLSCompliant(True) 放在项目中的每个类之前,还是它们都默认为 CLSCompliant?

If I have a project and I mark it as

<Assembly: CLSCompliant(True)> 

Do I need to put CLSCompliant(True) before every class in the project, or are they all defaulted to CLSCompliant?

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

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

发布评论

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

评论(2

平生欢 2024-08-19 02:49:10

在程序集级别添加 CLSCompliant 属性无论如何都不会更改类,因此它不会使类符合 CLS。它的作用是强制编译器将任何不合规的内容作为错误引发。确保课程不违反规则仍然是您的责任。

尽管如此,是的,它确实会过滤并影响程序集中的所有类,而无需将其再次添加到每个类中。 从此处

如果没有 CLS 属性应用于
类型,该类型被假定具有
与中的装配具有相同的 CLS 合规性
定义了类型。

这篇文章也是一篇相当不错的文章很好地概述了它的工作原理、您需要做什么以及为什么您可能想要这样做。

Adding the CLSCompliant attribute at the assembly level does not change the classes in anyway, so it does not make the classes CLS compliant. What it does is to force the compiler to raise anything that isn't compliant as an error. It is still your responsibility to actually make sure the classes don't break the rules.

That said though, yes it does filter down and affect all the classes in the assembly without the need to add it again to each class. From here:

If no CLS attribute is applied to a
type, that type is assumed to have the
same CLS compliance as the assembly in
which the type is defined.

This article is also a quite a nice overview on how it works, what you need to do, and why you may want to do it.

(り薆情海 2024-08-19 02:49:10

我对 CLSCompliant 的了解是,默认情况下您的程序集不兼容。在我看来,并非所有项目都需要符合 CLSCompliant。将由其他应用程序使用的工具或库项目应该符合 CLS。在构建适用于 Windows Live 的 Visual Studion 工具时,我们必须确保我们的代码不会给出任何 CLSCompliant 错误或警告。

What I know about CLSCompliant is, by default your assembly is not compliant. In my opinion, not all projects need to be CLSCompliant. Projects which are tools or libraries, that are going to be used by other applications should be CLSCompliant. While building Visual Studion Tools for Windows Live, we had to make sure our code did not give any CLSCompliant errors or warnings.

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