包含不符合 CLS 的参考的程序集是否可以符合 CLS?
我有一个不符合 CLS 的现有 DLL,我从自己的项目中引用了该 DLL。当我将程序集标记为符合 CLS 时,我收到编译器警告,指出引用的程序集中的名称不符合 CLS。
有没有一种方法可以让我的程序集符合 CLS 并将引用的程序集标记为不符合?
I have an existing DLL that is not CLS-compliant that I reference from my own project. When I mark my assembly as CLS-compliant, I get compiler warnings that names in the referenced assembly are not CLS-compliant.
Is there a way I can keep my assembly CLS-compliant and mark the referenced one as not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,只要您的 DLL 不从引用的程序集中公开任何不符合 CLS 的成员,即它不会在其自己的任何公共或受保护的成员或类型中提及它们,就可以符合 CLS 。 (它仍然可以在私有和内部成员和类型中使用它们。)
如果您的 DLL 确实需要直接从不兼容的 DLL 公开类型,您可以尝试将这些类型封装在您自己的包装器中(例如,方法可能返回 < code>MyWrapperAroundNaughtyType 而不是
NaughtyType
),或者您可以标记 API 的相关成员CLSCompliant(false)
以仅选择这些成员不进行编译器检查。Yes, your DLL can be CLS-compliant as long as it doesn't expose any non-CLS-compliant members from the referenced assembly -- that is, it doesn't mention them in any of its own public or protected members or types. (It can still use them in private and internal members and types.)
If your DLL does need to expose types directly from the non-compliant DLL, you can either try encapsulating those types in your own wrappers (e.g. a method might return a
MyWrapperAroundNaughtyType
instead of aNaughtyType
), or you can mark the relevant members of your APICLSCompliant(false)
to opt just those members out of compiler checking.由于其他建议不起作用,我最终所做的是将引用的 dll(具有不符合 CLS 的成员)的属性
Embed Interop Types
设置为 false。What I ended up doing because the other suggestions didn't work was setting the property
Embed Interop Types
of the referenced dll (with the non-CLS-compliant members) to false.