lundi 6 mars 2017

Get runtime getter of interface property

Declarations:

interface I
{
    int i { get; set; }
}

class C : I
{
    public int i { get; set; }
}

Code:

C c = new C();
c.i = 10;
PropertyInfo pi1 =
    c.
    GetType().
    GetInterfaces().
    SelectMany(t => t.GetProperties()).
    ToArray()[0];
PropertyInfo pi2 =
    c.
    GetType().
    GetProperties()[0];
object v1 = pi1.GetValue(c);
object v2 = pi2.GetValue(c);

Hey, v1 == v2, pi1 != pi2, but GetValue obviously calls same method. How can I know in my code that pi1 and pi2 call same method body?





Aucun commentaire:

Enregistrer un commentaire