什么时候可以打破CLS合规?

时间:2022-10-03 15:39:26

I was wondering which edge cases exist that could make Common Language Specification compliance acceptable. Even when not intending to be accessed from other languages, I think that the tenets asserted by the CLSCompliantAttribute are good best practices.

我想知道哪些边缘情况可以使公共语言规范合规性可以接受。即使不打算从其他语言访问,我认为CLSCompliantAttribute声明的原则是良好的最佳实践。

Do you have encountered / know of cases where YAGNI outweighs the best practices?

您是否遇到/了解YAGNI超过最佳做法的案例?

3 个解决方案

#1


"[sic] What use is there for being CLS compliant?"

“[原文如此]符合CLS的用途是什么?”

Medium trust, ClickOnce, running from a shared network drive, guest profiles in a domain setting, etc. There are lots of security situations where your code cannot run if you break the CLS Compliance.

中等信任,ClickOnce,从共享网络驱动器运行,域设置中的访客配置文件等。如果您违反CLS合规性,则会出现许多安全情况,导致代码无法运行。

I have personally seen a lot of situations where users are trying to run their application from a shared network drive and can't because the local admin has killed non-CLS compliant applications in the security profile.

我个人已经看到很多情况,用户试图从共享网络驱动器运行他们的应用程序而不能,因为本地管理员已经在安全配置文件中杀死了非CLS兼容的应用程序。

In general, there are usually ways to work around the issue anyway. I would take the opposite approach to the comments above, why break it? You are writing managed code, why would you want to limit your application on purpose?

一般来说,通常有办法解决这个问题。我会采取与上述评论相反的方法,为什么要打破它?您正在编写托管代码,为什么要故意限制应用程序?

I would say that if you are building an API assembly or component, you should always adhere to them. Too many third-party components take the easy way out and just flag them as broken when attempting to run from medium trust. In some cases, this is the only reason they can't run. If they had taken a little more time to adhere to the guidelines, users would not be limited as to how they can use their component.

我会说,如果您正在构建API程序集或组件,您应该始终遵守它们。太多的第三方组件采用简单的方法,只是在尝试从中等信任运行时将它们标记为已损坏。在某些情况下,这是他们无法运行的唯一原因。如果他们花了更多的时间来遵守指南,用户就不会限制他们如何使用他们的组件。

#2


Well, "params" arrays on attributes are sometimes just so tempting (but non-compliant). But I'd recommend using CLS-compliant approaches whenever possible.

好吧,属性上的“params”数组有时候很诱人(但不合规)。但我建议尽可能使用符合CLS的方法。

#3


I think it's acceptable for library internal to a product when working with legacy layers that require that kind of features or for performance reasons.

我认为在使用需要这种功能的遗留层或出于性能原因时,产品库内部是可以接受的。

But these non-conformant interfaces should then be reencapsulated at a higher level.

但是,这些不符合要求的接口应该在更高的层次上重新封装。

#1


"[sic] What use is there for being CLS compliant?"

“[原文如此]符合CLS的用途是什么?”

Medium trust, ClickOnce, running from a shared network drive, guest profiles in a domain setting, etc. There are lots of security situations where your code cannot run if you break the CLS Compliance.

中等信任,ClickOnce,从共享网络驱动器运行,域设置中的访客配置文件等。如果您违反CLS合规性,则会出现许多安全情况,导致代码无法运行。

I have personally seen a lot of situations where users are trying to run their application from a shared network drive and can't because the local admin has killed non-CLS compliant applications in the security profile.

我个人已经看到很多情况,用户试图从共享网络驱动器运行他们的应用程序而不能,因为本地管理员已经在安全配置文件中杀死了非CLS兼容的应用程序。

In general, there are usually ways to work around the issue anyway. I would take the opposite approach to the comments above, why break it? You are writing managed code, why would you want to limit your application on purpose?

一般来说,通常有办法解决这个问题。我会采取与上述评论相反的方法,为什么要打破它?您正在编写托管代码,为什么要故意限制应用程序?

I would say that if you are building an API assembly or component, you should always adhere to them. Too many third-party components take the easy way out and just flag them as broken when attempting to run from medium trust. In some cases, this is the only reason they can't run. If they had taken a little more time to adhere to the guidelines, users would not be limited as to how they can use their component.

我会说,如果您正在构建API程序集或组件,您应该始终遵守它们。太多的第三方组件采用简单的方法,只是在尝试从中等信任运行时将它们标记为已损坏。在某些情况下,这是他们无法运行的唯一原因。如果他们花了更多的时间来遵守指南,用户就不会限制他们如何使用他们的组件。

#2


Well, "params" arrays on attributes are sometimes just so tempting (but non-compliant). But I'd recommend using CLS-compliant approaches whenever possible.

好吧,属性上的“params”数组有时候很诱人(但不合规)。但我建议尽可能使用符合CLS的方法。

#3


I think it's acceptable for library internal to a product when working with legacy layers that require that kind of features or for performance reasons.

我认为在使用需要这种功能的遗留层或出于性能原因时,产品库内部是可以接受的。

But these non-conformant interfaces should then be reencapsulated at a higher level.

但是,这些不符合要求的接口应该在更高的层次上重新封装。