4
爲什麼當t是派生接口時,typeof(t).GetProperties()沒有找到t的所有公共屬性?這是預期的行爲還是我錯過了某些東西?typeof(t).GetProperties()當t是從另一個接口派生的接口
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分財產?
謝謝
恩裏克
感謝您的回答。現在很清楚。恩裏克 – user2230803