Translating a MethodInfo object obtained from an interface type, to the corresponding MethodInfo object on an implementing type in C#?

Look at Type. GetInterfaceMap Sorry - am in a rush so don't have time for a full answer but it should be a start.

Look at Type.GetInterfaceMap. Sorry - am in a rush so don't have time for a full answer but it should be a start.

Ok, let me do that. – Lasse V. Karlsen?

Oct 6 '09 at 11:57 Ooh, that was exactly what I needed, want to build an extension method on top of it for easy querying, but that hit the spot! – Lasse V. Karlsen?

Oct 6 '09 at 11:58 I'll post an answer of my own with the extension method and the edited code. – Lasse V. Karlsen?

Oct 6 '09 at 12:01.

For future reference, and if others are interested, the solution given to me by @Greg Beech here was to use Type.GetInterfaceMap. Here's the altered program code with an extension method at the bottom. Using System; using System.

Linq; using System. Reflection; using System. Diagnostics; namespace ConsoleApplication8 { public interface ITest { void Test(); } public class Test : ITest { void ITest.Test() { throw new NotImplementedException(); } } class Program { static void Main(string args) { Type interfaceType = typeof(ITest); Type classType = typeof(Test); InterfaceMapping map = classType.

GetInterfaceMap(interfaceType); MethodInfo testMethodViaInterface = interfaceType.GetMethods()0; MethodInfo implementingMethod = testMethodViaInterface. GetImplementingMethod(classType); Console.Out. WriteLine("interface: " + testMethodViaInterface.Name); if (implementingMethod!

= null) Console.Out. WriteLine("class: " + implementingMethod.Name); else Console.Out. WriteLine("class: unable to locate"); Console.Out.

Write("Press enter to exit..."); Console.In.ReadLine(); } } public static class TypeExtensions { /// /// Gets the corresponding object for /// the method in a class that implements a specific method /// from an interface. /// /// /// The for the method to locate the /// implementation of. /// /// The of the class to find the implementing /// method for.

/// /// /// The of the method that implements /// . /// /// /// is null. /// - or - /// is null.

/// /// /// is not defined in an interface. /// public static MethodInfo GetImplementingMethod(this MethodInfo interfaceMethod, Type classType) { #region Parameter Validation if (Object. ReferenceEquals(null, interfaceMethod)) throw new ArgumentNullException("interfaceMethod"); if (Object.

ReferenceEquals(null, classType)) throw new ArgumentNullException("classType"); if (!interfaceMethod.DeclaringType. IsInterface) throw new ArgumentException("interfaceMethod", "interfaceMethod is not defined by an interface"); #endregion InterfaceMapping map = classType. GetInterfaceMap(interfaceMethod.

DeclaringType); MethodInfo result = null; for (Int32 index = 0; index InterfaceMethods. Length; index++) { if (map. InterfaceMethodsindex == interfaceMethod) result = map.

TargetMethodsindex; } Debug. Assert(result! = null, "Unable to locate MethodInfo for implementing method"); return result; } } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions