Why does C # Type.GetProperty () behave differently for interfaces than for base classes?

Why Type.GetProperty(string)doesn't it get the property from the base interface if the type is an interface? For example, the following code prints:

System.String X
null
System.String X
System.String X

which seems inconsistent:

void Main()
{
    Console.WriteLine(typeof(I1).GetProperty("X"));
    Console.WriteLine(typeof(I2).GetProperty("X"));
    Console.WriteLine(typeof(C1).GetProperty("X"));
    Console.WriteLine(typeof(C2).GetProperty("X"));;
}

public interface I1 { string X { get; } }

public interface I2 : I1 { }

public class C1 { public string X { get { return "x"; } } }

public class C2 : C1 { }

EDIT: Another aspect of the runtime that supports Cole's answer is this:

public class C : I2 {
    // not allowed: the error is
    // 'I2.X' in explicit interface declaration is not a member of interface
    string I2.X { get; set; }

    // allowed
    string I1.X { get; set; }
}
+5
source share
1 answer

Remember that class inheritance does not match interface implementations.

is-a. D : B, D B. B , D , , ; "" D B.

, , ID : IB, ID IB , . ? ID IB - ; . . : ", ID, IB."

, ID IB, ID, . , , promises , ID, .

, IB X, "does ID X?" . ID IB, X, X.

+6

All Articles