TypeDescriptor不会从inheritance的接口返回成员

我的问题是TypeDescriptor不会从inheritance的接口返回成员,这是它应该如何工作? 还是一个bug?

[TestFixture] public class DescriptorTests { [Test] public void Test() { // count = 1 Assert.AreEqual(2, TypeDescriptor.GetProperties(typeof(IFoo)).Count); // it is going to fail, the Id is not going to be returned } public interface IEntity { int Id { get; set; } } public interface IFoo : IEntity { string Name { get; set; } } } 

这不是一个错误。 从ECMA CLI规范 :

8.9.11接口类型推导

接口类型可能需要实现一个或多个其他接口。 任何实现对接口类型的支持的类型也应实现对该接口指定的任何所需接口的支持。 这与对象类型inheritance有两种不同:

  • 对象类型形成单个inheritance树; 接口类型没有。
  • 对象类型inheritance指定如何inheritance实现; 必需的接口没有,因为接口没有定义实现。 必需的接口指定实现对象类型应支持的其他合同。

要突出显示最后的差异,请考虑具有单个方法的接口IFoo 。 从它派生的接口IBar要求支持IBar任何对象类型也支持IFoo 。 它没有说明IBar本身将拥有哪些方法。

8.10成员inheritance

只有对象类型可以inheritance实现,因此只有对象类型才能inheritance成员(参见§8.9.8)。 虽然接口类型可以从其他接口类型派生,但它们只“inheritance”实现方法契约的要求,而不是字段或方法实现。

编辑…

如果你想获得一个接口的属性,包括它的祖先的属性,那么你可以做这样的事情:

 var properties = typeof(IFoo) .GetProperties() .Union(typeof(IFoo) .GetInterfaces() .SelectMany(t => t.GetProperties())); 

你是对的。 我认为这是一个错误,因为它适用于类的inheritance属性!