c# - Identifying properties that override base properties or implement interfaces -


I've been given:

  • A class " T < / Code> "which implements the properties decorated with those qualities which I am interested in.

  • A PropertyInfo " p " (1) An interface related to " I " Representing a property, which implements T , (2) a base class " B " inherited from T , Or (3) class T .

  • I (or property in T), even if the declared declaration of p is an interface that implements T Or a class that is derived from T That's why I want a way to get from p to PropertyInfo which is actually present on T . Thus it seems that there are 3 cases:

    1. p.DeclaringType == type (t)

      this is a trivial matter . By the time I do not find a match, I can repeat through the properties of T . Example:

        Return Type (T) .GetProperties (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic). FirstOrDefault (property = & gt; property == p)  
    2. p.DeclaringType == type (I) where I is an interface that implements T

      This is more complex. I found that I can use an interface mapping to find the related implementation of p in p . It seems like this, but what works here:

        if (p.DeclaringType.IsInterface & Typef (T) .GetInterfaces (). This includes (P. Delivering Type)) {interface mapping map = typef (t) .GetInterfaceMap (p.DeclaringType); MethodInfo getMethod = p.GetGetMethod (); (Int i = 0; I & lt; map.InterfaceMethods.Length; i ++) {If (map.InterfaceMethods [i] == getMethod) {MethodInfo target = map.TargetMethods [i]; Return Typef (T) .GetProperties (BindingFlag.Instens | BindingFlag.Policy | Compulsive Flag Nonpolic) .FirstOrDefault (property = & gt; property.GetGetMethod () == Target); }}  
    3. p.DeclaringType == type (b) where b is a class T gets

      I have not yet figured out how to override the virtual property to identify the property on T < Code> p defined T s base class b .

    So my questions are

    • in Case 3 on PropertyInfo How can I get code> T that property to p on b ?
    • Is there a better way of doing all three? Perhaps the way to unite three cases.
    • I think you are missing a case: it is possible that Do not overwrite property with B , not overwritten (unless you are not certain that it will not be in your specific scenario).

      Anyway, you have to search for properties by name, using typef (t). GETProperty , because in a given class there can not be 2 properties with the same name (unlike the methods, which can have many overloads). However, you need to check this case where inherited from T hidden property B , new Modifier To handle that case, you can see the property of the Recipient Method (derived from the GetGetMethod ) property: If the NewSlot flag exists, So the heritage hides the recipient.


Comments