我有一个System.Type的实例,它表示一个接口,我想得到该接口上所有属性的列表,包括从基接口继承的属性。我基本上希望从接口中获得与类相同的行为。
例如,给定此层次结构:
public interface IBase {
public string BaseProperty { get; }
}
public interface ISub : IBase {
public string SubProperty { get; }
}
public class Base : IBase {
public string BaseProperty { get { return "Base"; } }
}
public class Sub : Base, ISub {
public string SubProperty { get { return "Sub"; } }
}
如果我在类上调用getproperties--
typeof(Sub).GetProperties()
--然后我得到baseproperty和subproperty。我想对接口做同样的事情,但当我尝试它时--
typeof(ISub).GetProperties()
--回来的只是子属性。
我试过了
BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy
对于getproperties,因为我对flattenhierarchy的理解是它应该包括来自基类的成员,但是行为是完全相同的。
我想我可以重复一下
Type.GetInterfaces()
并对每一个调用getproperties,但随后我将依赖接口上的getproperties
从未
返回基本属性(因为如果有,我会得到重复的属性)。我宁愿不依赖这种行为,至少在没有看到记录的情况下。
我怎样才能:
-
获取一个接口上所有属性的列表,包括来自它的基本接口的属性?或
-
至少要有信心,我看到的是我可以依赖的有文档记录的行为,所以我可以解决它?