是C# 4.0的新特性——“可选参数”符合 CLS?
这个新功能确实很方便。
最近我读到“Microsoft All-In-One Code Framework”的文档,其中提到“OptionalParameters”不符合CLS。
所以我通过在公共API中使用“可选参数”对其进行了测试,并打开了FxCop,然后我进行了编译,FxCop没有抱怨任何事情。与此同时,当我添加一个以 uint 作为返回类型的 API 时,FxCop 确实报告了一条警告。
所以现在我很困惑,“可选参数”是否符合 CLS?
确定新语言功能是否符合 CLS 的最佳方法是什么?
This new feature is really convenient.
Lately I read the document of the "Microsoft All-In-One Code Framework", and it mentions that "Optional Parameters" is not CLS-Compliant.
So I tested it by using "Optional Parameters" in a public API, and turned on FxCop, then I compiled and FxCop did not complain about anything. At the mean while, FxCop did report a warning when I add an API that has uint as its return type.
So now I am confused, is "Optional Parameters" CLS-Compliant or not?
And what's the best way to find out whether a new language feature is CLS-Compliant or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可选参数“有点”符合 CLS。带有可选参数的方法是合法的,并且可以使用
CLSCompliant
属性成功编译,但是这些方法的调用者不一定需要考虑默认参数值或可选属性。 (在这种情况下,这些方法的行为方式与标准方法完全相同,要求在调用站点显式声明所有参数。)(摘自“CA1026:不应使用默认参数”的文档。)
Optional arguments are "sort-of" CLS-compliant. Methods with optional arguments are legal and can be successfully compiled with the
CLSCompliant
attribute, but callers of those methods don't necessarily need to take account of the default parameter values or the optional attribute. (In which case those methods would behave in exactly the same way as standard methods, requiring that all the arguments be stated explicitly at the call site.)(Taken from the documentation for "CA1026: Default parameters should not be used".)
我将您的问题解释为关于可选参数。
如果是这样,那么我相信它们符合 CLS,您可以使用 CLSCompliant 属性:
编译时不会出现警告。
I interpret your question to be about Optional Arguments.
If so then I believe they are CLS-Compliant and you can check by using the CLSCompliant attribute:
This compiles with no warnings.
请查看 CLS 规范。< br>
从第 41 页开始:
但下面的方框说:
Have a look at the CLS specs.
From page 41:
But the box right below says: