我有一个 .NET 3.5 C# 项目,其命名空间为 SampleNamespace.Tools.Sample
。
如果我将名为“Samplenamespace.Utils.Example”的程序集添加到我的项目中,我会收到以下警告:
标识符“Samplenamespace”仅在大小写不符合 CLS 时有所不同
请注意 Samplenamespace
中的小写“n”。
目前我什至没有在我的项目中使用参考程序集。 只需将其添加为参考就会导致警告。
考虑到我什至没有在公共类中公开对程序集的任何引用,为什么编译器会抱怨这一点?
有什么解决办法吗?
I have a .NET 3.5 C# project that has a namespace of SampleNamespace.Tools.Sample
.
If I add an assembly called "Samplenamespace.Utils.Example" to my project I get the following warning:
Identifier 'Samplenamespace' differing only in case is not CLS-compliant
Note the lower case 'n' in Samplenamespace
.
I'm not even using the reference assembly in my project at the moment. Simply adding it as a reference causes the warning.
Why is the compiler complaining about this considering I'm not even exposing any references to the assembly in my public classes?
Any workaround?
发布评论
评论(2)
当您具有像这样的混合命名空间时,并非所有 .NET 语言都区分大小写(例如 VB),仅在其他开发人员可能无法访问您的代码的情况下(使用警告的措辞)进行比较。
这可能不是你的情况,这就是为什么它是一个警告(在我的商店中我们将其视为错误)
Not all .NET languages are case sensitive (VB for example) when you have mixed namespaces like this, diffing only in case (to use the wording of the warning) your code may not be accessable to other developers.
That may not be your case, which is why it's a warning (which in my shop we treat as an error)
它只是警告您,因为并非所有可以使用解决方案中的类型的语言都会意识到这种差异(并且可能无法使用这些类型)。
我认为您可以通过将程序集标记为不符合 CLS(在 AssemblyInfo.cs 文件中)来避免此警告 (在此处阅读更多信息):
不确定我认为这是一个好主意...
更新:我认为警告的原因是尽管没有公开任何内容,但发布的是命名空间没有访问修饰符。 您也许可以说命名空间始终是公共的,因此它们暴露给潜在的客户端,即使它们可能不包含任何公共类型。
It is simply warning you since not all languages that can consume the types within your solution will be aware of the difference (and may be unable to use the types).
I think that you can avoid this warning by marking your assembly as being non-CLS compliant (in the AssemblyInfo.cs file) (read more here):
Not sure I think it's a good idea though...
Update: I think that the reason that the warning is issued though nothing is publicly exposed is that namespaces do not have access modifiers. You could perhaps say that namespaces are always public, so they are exposed to potential clients, even though they may not contain any public types.