locked
Finding a concrete methods interface definition using reflection RRS feed

  • Question

  •  

    Given that I have a MethodInfo object from a concrete class that implicitly implements an interface method, what is the simplest way to find the corresponding methodinfo from the interface that it implements?

     

    The interfacamapping can go from interface and /to/ concrete method, but I can't find a way to go from the concrete method and back. Currently I'm iterating the interface methods comparing names and parameter lists to figure out if they are the same.

     

     

    Wednesday, May 14, 2008 11:20 AM

Answers

  • Try this out... Basically, we get the Type from the MethodInfo, then cycle through it's implemented interfaces, then create InterfaceMappings, and return the MethodInfo that matches our MethodInfo.

     

    This approach is a little more solid than checking name and parameters, and probably faster.

     

    The first example finds the first matching interface implementation.

     

    Code Snippet

    static MethodInfo GetInterfaceMethod(MethodInfo concreteMethod)

    {

    // get the type that the method is defined in

    Type classType = concreteMethod.ReflectedType;

     

    // get the interfaces that the type implements

    Type[] interfaces = classType.GetInterfaces();

     

    // iterate through each interface creating mappings,

    // looking for a match to our MethodInfo

    foreach (Type interfaceType in interfaces)

    {

    // get the mapping for the interface.

    InterfaceMapping map = classType.GetInterfaceMap(interfaceType);

     

    // iterate through the MethodInfos

    for (int i = 0; i < map.TargetMethods.Length; i++)

    {

    // look for a match to the passed in MethodInfo

    if (map.TargetMethods[i] == concreteMethod)

    {

    // return the corresponding interface method.

    return map.InterfaceMethods[i];

    }

    }

    }

     

    // return null if no match was found.

    return default(MethodInfo);

    }

     

     

     

    The second example deals with the fact that a concrete method could actually implement multiple interface methods if the class type implements multiple interfaces which have matching method signatures in them.

     

     

    Code Snippet

     

    static IEnumerable<MethodInfo> GetInterfaceMethods(MethodInfo concreteMethod)

    {

    // get the type that the method is defined in

    Type classType = concreteMethod.ReflectedType;

     

    // get the interfaces that the type implements

    Type[] interfaces = classType.GetInterfaces();

     

    // iterate through each interface creating mappings,

    // looking for a match to our MethodInfo

    foreach (Type interfaceType in interfaces)

    {

    // get the mapping for the interface.

    InterfaceMapping map = classType.GetInterfaceMap(interfaceType);

     

    // iterate through the MethodInfos

    for (int i = 0; i < map.TargetMethods.Length; i++)

    {

    // look for a match to the passed in MethodInfo

    if (map.TargetMethods[i] == concreteMethod)

    {

    // return the corresponding interface method.

    yield return map.InterfaceMethods[i];

    }

    }

    }

    }

     

     


    Hope that helps,

    Troy

     

    Wednesday, May 14, 2008 9:17 PM