c#引用inheritance自c的用法b

我有A,B,C类这样的:

//Class A is in Console application that has reference to class library that has class B public class A { public void UseBObject() { B BInstance=new B(); // error C is defined in assembly that is not referenced you must add reference that contains C... } } //Class library that reference another library the contains C class public class B : C { public string Name{get;set;} } 

我的问题是,如果B已经引用了C而A引用了B为什么A需要引用C? 这没有意义吗?

假设类C定义如下,它定义了一个名为PropertyInC

 public class C { public string PropertyInC {get;set;} } 

然后当通过B的实例使用C的公共成员时就像这样。

 public void UseBObject() { B BInstance=new B(); BInstance.PropertyInC = "Whatever"; } 

为什么编译器会知道什么是PropertyInC ? 你有什么类型的线索吗? 或者编译器如何有机会知道这样的属性是否存在?

好吧,你可以争辩说编译器可以使用AsselblyB(B所在的程序集)的程序集引用找到它,但是不能保证引用的程序集会在同一条路径中存在或者无论如何。 如果不存在,它将在运行时失败:)

另请注意,如果您没有inheritance关系,编译器将允许您编译,但是如果您需要访问b.CMember您肯定会添加对C所在的程序集的引用。

 public class B { private C CMember{get;set;}//This will compile public string Name{get;set;} } 

希望这可以帮助。

因为B是C,所以要创建C,A需要知道它,因为它是B的公共表面区域的一部分。