CLSCompliant(真)拖入未使用的引用 [英] CLSCompliant(true) drags in unused references

查看:851
本文介绍了CLSCompliant(真)拖入未使用的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以的解释的以下行为?

Can anyone explain the following behavior?

总之,如果你创建多个的符合CLS 的在Visual Studio 2008,并让他们都有一个共同的名字空间的根库,出库引用另一个库将的需要的引用了图书馆的引用,即使它没有使用它们。

In summary, if you create multiple CLS compliant libraries in Visual Studio 2008 and have them share a common namespace root, a library referencing another library will require references to that library's references even though it doesn't consume them.

这是pretty的难以用一句话来解释,但这里有步骤来重现行为(要密切注意的命名空间):

It's pretty difficult to explain in a single sentence, but here are steps to reproduce the behavior (pay close attention to the namespaces):

创建一个名为LibraryA的库并添加AA单级到库:

Create a library called LibraryA and add a a single class to that library:

namespace Ploeh
{
    public abstract class Class1InLibraryA
    {
    }
}

请确保该库是符合CLS加入 [总成:CLSCompliant(真)。来的AssemblyInfo.cs

Make sure that the library is CLS Compliant by adding [assembly: CLSCompliant(true)] to AssemblyInfo.cs.

创建一个名为LibraryB和参考LibraryA的另一个库。添加下列类LibraryB:

Create another library called LibraryB and reference LibraryA. Add the following classes to LibraryB:

namespace Ploeh.Samples
{
    public class Class1InLibraryB : Class1InLibraryA
    {
    }
}

namespace Ploeh.Samples
{
    public abstract class Class2InLibraryB
    {
    }
}

请确保LibraryB也符合CLS。

Make sure that LibraryB is also CLS Compliant.

注意Class1InLibraryB从LibraryA的一种派生,而Class2InLibraryB没有。

Notice that Class1InLibraryB derives from a type in LibraryA, whereas Class2InLibraryB does not.

现在创建一个名为LibraryC和参考LibraryB(但不是LibraryA的)第三个图书馆。添加下面的类:

Now create a third library called LibraryC and reference LibraryB (but not LibraryA). Add the following class:

namespace Ploeh.Samples.LibraryC
{
    public class Class1InLibraryC : Class2InLibraryB
    {
    }
}

这应该还是编译。请注意,Class1InLibraryC从类LibraryB了的导出不使用任​​何类型从LibraryA的的。

This should still compile. Notice that Class1InLibraryC derives from the class in LibraryB that doesn't use any types from LibraryA.

此外请注意Class1InLibraryC的是在LibraryB定义的命名空间层次结构的一部分命名空间中定义的。

Also notice that Class1InLibraryC is defined in a namespace that is part of the namespace hierarchy defined in LibraryB.

到目前为止,LibraryC没有提到LibraryA的,而且由于它没有使用类型从LibraryA的,该解决方案编译。

So far, LibraryC has no reference to LibraryA, and since it uses no types from LibraryA, the solution compiles.

现在让LibraryC符合CLS也是如此。突然,该解决方案不再编译,给你这个错误信息:

Now make LibraryC CLS compliant as well. Suddenly, the solution no longer compiles, giving you this error message:

类型Ploeh.Class1InLibraryA'中未被引用的组件被定义。您必须添加一个引用程序集Ploeh,版本= 1.0.0.0,文化=中性公钥=空'。

The type 'Ploeh.Class1InLibraryA' is defined in an assembly that is not referenced. You must add a reference to assembly 'Ploeh, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

您可以使溶液中的下列方式之一重新编译:

You can make the solution compile again in one of the following ways:

  • 从LibraryC删除CLS遵从
  • 添加引用LibraryA的(虽然你并不需要它)
  • 更改LibraryC的命名空间,以便它不是LibraryB的命名空间层次结构的一部分(例如,为了Fnaah.Samples.LibraryC)
  • 更改Class1InLibraryB的命名空间(也就是一个的不是的从LibracyC使用),因此它是不在于LibraryC的命名空间层次结构(例如,为了Ploeh.Samples.LibraryB)
  • Remove CLS Compliance from LibraryC
  • Add a reference to LibraryA (although you don't need it)
  • Change the namespace in LibraryC so that it is not part of LibraryB's namespace hierarchy (e.g. to Fnaah.Samples.LibraryC)
  • Change the namespace of Class1InLibraryB (that is, the one not used from LibracyC) so that it is does not lie in LibraryC's namespace hierarchy (e.g. to Ploeh.Samples.LibraryB)

这似乎是有命名空间的层次结构与CLS遵从性的一些奇怪的相互作用。

It seems that there is some strange interplay between the namespace hierarchy and CLS compliance.

解决这个问题可以通过采摘在上面的列表中的一个选项来完成,但是任何人都可以解释的的原因的这种行为背后?

Solving this issue can be done by picking one of the options in the list above, but can anyone explain the reason behind this behavior?

推荐答案

我看看到了CLS的官方文件(<一href="http://msdn.microsoft.com/en-us/netframework/aa569283.aspx">http://msdn.microsoft.com/en-us/netframework/aa569283.aspx),但我的头爆炸之前,我能找到一个简单的答案。

I had a look into the official documents for the CLS (http://msdn.microsoft.com/en-us/netframework/aa569283.aspx), but my head exploded before I could find a simple answer.

但我认为基础是编译器,为了验证LibraryC的CLS遵从性,需要寻找与LibraryA的可能的命名冲突。

But I think the basis is that the compiler, in order to verify the CLS compliance of LibraryC, needs to look into possible naming conflicts with LibraryA.

编译器必须验证所有一类的部件都可以访问或外部可见定义组件(CLS规则1)。

The compiler must verify all "parts of a type that are accessible or visible outside of the defining assembly" (CLS Rule 1).

由于公共类Class1InLibraryC继承Class2InLibraryB,它必须验证针对LibraryA的CLS的合规性为好,特别是因为Ploeh。*,范围现在是CLS第5条在符合CLS的范围内引入的所有名称应是一种独特的独立。

Since public class Class1InLibraryC inherits Class2InLibraryB, it must verify the CLS compliance against LibraryA as well, in particular because "Ploeh.*" is now "in scope" for CLS Rule 5 "All names introduced in a CLS-compliant scope shall be distinct independent of kind".

更改Class1InLibraryB或Class1InLibraryC的任何命名空间,使他们成为独特的,似乎说服编译器就没有机会了名称冲突了。

Changing either the namespace of Class1InLibraryB or Class1InLibraryC so they become distinct seems to convince the compiler there is no chance for a name conflict anymore.

如果您选择选项(2),添加引用和编译,你会看到引用未竟标中所得到的组件的元数据,所以这只是一个编译/验证时依赖性。

If you choose option (2), add the reference and compile, you'll see that the reference is not actually marked in the resulting assembly meta-data, so this is a compilation/verification-time dependency only.

这篇关于CLSCompliant(真)拖入未使用的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆