30

More specifically, if I have:

public class TempClass : TempInterface
{

    int TempInterface.TempProperty
    {
        get;
        set;
    }
    int TempInterface.TempProperty2
    {
        get;
        set;
    }

    public int TempProperty
    {
        get;
        set;
    }
}

public interface TempInterface
{
    int TempProperty
    {
        get;
        set;
    }
    int TempProperty2
    {
        get;
        set;
    }
}

How do I use reflection to get all the propertyInfos for properties explicitly implementing TempInterface?

Thanks.

Nosredna
  • 83,000
  • 15
  • 95
  • 122
Dane O'Connor
  • 75,180
  • 37
  • 119
  • 173
  • Can you clarify exactly what you're looking for? Do you just want a list of all properties explicitly implemented by TempClass? Or do you just want the properties of TempInterface? etc. – Jacob Carpenter Nov 10 '08 at 21:18
  • 1
    I want a list of all the properties explicitly implemented by TempClass – Dane O'Connor Nov 10 '08 at 21:49

9 Answers9

28

I think the class you are looking for is System.Reflection.InterfaceMapping.

Type ifaceType = typeof(TempInterface);
Type tempType = typeof(TempClass);
InterfaceMapping map = tempType.GetInterfaceMap(ifaceType);
for (int i = 0; i < map.InterfaceMethods.Length; i++)
{
    MethodInfo ifaceMethod = map.InterfaceMethods[i];
    MethodInfo targetMethod = map.TargetMethods[i];
    Debug.WriteLine(String.Format("{0} maps to {1}", ifaceMethod, targetMethod));
}
5

The property getter and setter of an explicitly implemented interface property has an unusual attribute. It's IsFinal property is True, even when it is not a member of a sealed class. Try this code to verify my assertion:

  foreach (AssemblyName name in Assembly.GetEntryAssembly().GetReferencedAssemblies()) {
    Assembly asm = Assembly.Load(name);
    foreach (Type t in asm.GetTypes()) {
      if (t.IsAbstract) continue;
      foreach (MethodInfo mi in t.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)) {
        int dot = mi.Name.LastIndexOf('.');
        string s = mi.Name.Substring(dot + 1);
        if (!s.StartsWith("get_") && !s.StartsWith("set_")) continue;
        if (mi.IsFinal)
          Console.WriteLine(mi.Name);
      }
    }
  }
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
4

Here's a modified solution based on the implementation given in this blog post:

var explicitProperties =
    from prop in typeof(TempClass).GetProperties(
        BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)
    let getAccessor = prop.GetGetMethod(true)
    where getAccessor.IsFinal && getAccessor.IsPrivate
    select prop;

foreach (var p in explicitProperties)
    Console.WriteLine(p.Name);
Jacob Carpenter
  • 4,134
  • 1
  • 29
  • 30
3

Building on the answer by MrKurt:

var targetMethods =
    from iface in typeof(TempClass).GetInterfaces()
    from method in typeof(TempClass).GetInterfaceMap(iface).TargetMethods
    select method;

var explicitProps =
    from prop in typeof(TempClass).GetProperties(BindingFlags.Instance |
                                                 BindingFlags.NonPublic)
    where targetMethods.Contains(prop.GetGetMethod(true)) ||
          targetMethods.Contains(prop.GetSetMethod(true))
    select prop;
Community
  • 1
  • 1
dtb
  • 213,145
  • 36
  • 401
  • 431
1

A simple helper class that could help:

public class InterfacesPropertiesMap
{
    private readonly Dictionary<Type, PropertyInfo[]> map;

    public InterfacesPropertiesMap(Type type)
    {
        this.Interfaces = type.GetInterfaces();
        var properties = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);

        this.map = new Dictionary<Type, PropertyInfo[]>(this.Interfaces.Length);

        foreach (var intr in this.Interfaces)
        {
            var interfaceMap = type.GetInterfaceMap(intr);
            this.map.Add(intr, properties.Where(p => interfaceMap.TargetMethods
                                                    .Any(t => t == p.GetGetMethod(true) ||
                                                              t == p.GetSetMethod(true)))
                                         .Distinct().ToArray());
        }
    }

    public Type[] Interfaces { get; private set; }

    public PropertyInfo[] this[Type interfaceType]
    {
        get { return this.map[interfaceType]; }
    }
}

You'll get properties for each interface, even explicitly implemented.

lorond
  • 3,856
  • 2
  • 37
  • 52
1

It's overly complex. You have to reflect over the methods/properties of the Interface type, see if they exist in your class type, and compare them to see if they're the "same" when they do exist.

If something is in the interface but not the type you're testing, it's an explicit implementation. If it's in both, but different between the two, it's an explicit interface.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
MrKurt
  • 5,080
  • 1
  • 23
  • 22
  • If a class implements an interface, it implements all properties and methods in that interface. So you don't have to examine the members of the class, you just have to examine the members of the interface. – Robert Rossney Nov 10 '08 at 20:38
  • You have to compare the interface members to the class members to find out if the member is *explicitly* implemented or not. – MrKurt Nov 10 '08 at 23:17
1

I had to modify Jacob Carpenter's answer but it works nicely. nobugz's also works but Jacobs is more compact.

var explicitProperties =
from method in typeof(TempClass).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
where method.IsFinal && method.IsPrivate
select method;
Dane O'Connor
  • 75,180
  • 37
  • 119
  • 173
  • Your answer is getting methods, Jacob's is getting the properties as you asked in the question originally. – nawfal May 12 '13 at 13:32
0

This seems a bit painful for no apparent reason!

My solution is for the case where you know the name of the property you are looking for and is pretty simple.

I have a class for making reflection a bit easier that I just had to add this case to:

public class PropertyInfoWrapper
{
    private readonly object _parent;
    private readonly PropertyInfo _property;

    public PropertyInfoWrapper(object parent, string propertyToChange)
    {
        var type = parent.GetType();
        var privateProperties= type.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance);

        var property = type.GetProperty(propertyToChange) ??
                       privateProperties.FirstOrDefault(p => UnQualifiedNameFor(p) == propertyName);

        if (property == null)
            throw new Exception(string.Format("cant find property |{0}|", propertyToChange));

        _parent = parent;
        _property = property;
    }

    private static string UnQualifiedNameFor(PropertyInfo p)
    {
        return p.Name.Split('.').Last();
    }

    public object Value
    {
        get { return _property.GetValue(_parent, null); }
        set { _property.SetValue(_parent, value, null); }
    }
}

You cant just do == on name because explicitly implemented properties have fully qualified names.

GetProperties needs both the search flags to get at private properties.

JonnyRaa
  • 7,559
  • 6
  • 45
  • 49
-1

Jacob's code is missing a filter:

        var props = typeof(TempClass).GetInterfaces().Where(i => i.Name=="TempInterface").SelectMany(i => i.GetProperties());
        foreach (var prop in props)
            Console.WriteLine(prop);
Robert Rossney
  • 94,622
  • 24
  • 146
  • 218