Typeof (t) .GetProperties () when t is an interface that comes from another

why typeof (t). GetProperties () does not find all the public properties of t when t is a derived interface? Is this expected behavior or is something missing?

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 );
    }
}

Result: From IBaseOne: 1 property From property IDerivedOne: 1 From properties ImplementsIDerivedOne: 2

Why does IDerivedOne show only 1 property?

Thank you

Enrique

+4
source share
1 answer

This is because interfaces do not "produce" one from another; think of them as contracts that class classes must fulfill. Therefore, when you have this:

interface IFoo : IBar { }

, IFoo , IBar. , IFoo IBar. , , .

, IDerivedOne, IDerivedOne, IDerivedOne, "" .

+5

Source: https://habr.com/ru/post/1526502/


All Articles