从多个接口为何集合类在C#(如ArrayList)继承,如果这些接口中的一个从剩下的继承? [英] Why collections classes in C# (like ArrayList) inherit from multiple interfaces if one of these interfaces inherits from the remaining?
问题描述
当我美元的ArrayList的关键字p $ PSS F12去从VS2008产生对元数据,我发现生成的类声明如下:
When I press f12 on the ArrayList keyword to go to metadata generated from vs2008, I found that the generated class declaration as follows
public class ArrayList : IList, ICollection, IEnumerable, ICloneable
我知道的IList已自ICollection和IEnumerable继承,所以为什么ArrayList的冗余从这些接口继承?
I know that the IList already inherits from ICollection and IEnumerable, so why does ArrayList redundantly inherit from these interfaces?
推荐答案
好了,我做了一些研究。如果您创建了以下层次:
OK, I've done some research. If you create the following hierarchy:
public interface One
{
void DoIt();
}
public interface Two : One
{
void DoItMore();
}
public class Magic : Two
{
public void DoItMore()
{
throw new NotImplementedException();
}
public void DoIt()
{
throw new NotImplementedException();
}
}
和编译它,然后引用的DLL在不同的解决方案,请键入魔术和preSS F12,您将获得以下内容:
And compile it, then reference the DLL in a different solution, type Magic and Press F12, you will get the following:
public class Magic : Two, One
{
public Magic();
public void DoIt();
public void DoItMore();
}
您将看到的界面层级扁平,或者编译器中添加的接口?如果你使用反射,你得到相同的结果了。
You will see that the interface hierarchy is flattened, or the compiler is adding the interfaces in? If you use reflector you get the same results too.
更新:如果您在ILDASM打开DLL,你会看到它说:
Update: If you open the DLL in ILDASM, you will see it saying:
工具...两个
工具...一。
这篇关于从多个接口为何集合类在C#(如ArrayList)继承,如果这些接口中的一个从剩下的继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!