<分区>
当 t 是派生接口(interface)时,为什么 typeof(t).GetProperties() 没有找到 t 的所有公共(public)属性?这是预期的行为还是我遗漏了什么?
public interface IBaseOne
{ int Id { get; } }
public interface IDerivedOne : IBaseOne
{ string Name { get; } }
public class ImplementsIDerivedOne : IDerivedOne
{
public int Id { get; private set; }
public string Name { get; private set; }
}
public static class TypeOfTests
{
public static Type Testing<T>() where T : class,IBaseOne
{
return typeof(T);
}
}
class Program
{
static void Main(string[] args)
{
Type typeFromIBaseOne = TypeOfTests.Testing<IBaseOne >() ;
Type typeFromIDerivedOne = TypeOfTests.Testing<IDerivedOne>();
Type typeFromImplementsIDerivedOne = TypeOfTests.Testing<ImplementsIDerivedOne>();
PropertyInfo[] propsFromIBaseOne = typeFromIBaseOne.GetProperties();
PropertyInfo[] propsFromIDerivedOne = typeFromIDerivedOne.GetProperties();
PropertyInfo[] propsFromImplementsIDerivedOne =TypeFromImplementsIDerivedOne.GetProperties();
Debug.Print("From IBaseOne: {0} properties", propsFromIBaseOne.Length);
Debug.Print("From IDerivedOne: {0} properties", propsFromIDerivedOne.Length);
Debug.Print("From ImplementsIDerivedOne: {0} properties", propsFromImplementsIDerivedOne .Length );
}
}
结果: 来自 IBaseOne:1 个属性 来自 IDerivedOne:1 个属性 来自 ImplementsIDerivedOne:2 个属性
为什么 IDerivedOne 只显示 1 个属性?
谢谢
恩里克