Retrieving the MethodInfo of of the correct overload of a generic method?

This makes it much easier: public static MethodInfo GetMethod(Expression action) { var body = action. Body as MethodCallExpression; return body.Method. GetGenericMethodDefinition(); } TestMethod public void Test1() { var method = GetMethod(foo => foo.

Bar((Func)null); Assert. IsNotNull(method); }.

Wow.. that's cool. Works great! And so little code.

– Anne Jan 6 at 19:57.

I don't think you can do this directly using GetMethod. I suspect you'll have to iterate over all the methods called Bar, then: Check that the method has one type parameter Check that the method has one normal parameter Use the type parameter to make a Func (with typeof(Func). MakeGenericType) and check that the parameter type matches that.

LINQ is good for this sort of thing. Complete sample: using System; using System. Reflection; using System.

Linq; public class Foo { public void Bar(Func f) { } public void Bar(Action a) { } } class Test { static void Main() { var methods = from method in typeof(Foo).GetMethods() where method. Name == "Bar" let typeArgs = method. GetGenericArguments() where typeArgs.

Length == 1 let parameters = method.GetParameters() where parameters. Length == 1 where parameters0. ParameterType == typeof(Func).

MakeGenericType(typeArgs0) select method; Console. WriteLine("Matching methods..."); foreach (var method in methods) { Console. WriteLine(method); } } } Basically generics and reflection are really nasty in combination, I'm afraid :(.

Surprisingly, it looks like you'll need to call GetMethods() and loop over the methdos ntil you find the one you want. For example: var yourMethod = typeof(Foo).GetMethods() . First(m => m.Name == "Bar" && m.GetParameters().

Length == 1 && m.GetParameters()0.ParameterType. ContainsGenericParameters && m.GetParameters()0.ParameterType. GetGenericTypeDefinition() == typeof(Func)).

It works. Thanks. – Anne Dec 14 '10 at 19:54.

You'll struggle with just with just GetMethod - you could try something along the lines of; var method = (from m in typeof(Foo).GetMethods() where m. IsGenericMethodDefinition == true && m. Name == "Bar" && m.GetParameters().

Length > 0 && m.GetParameters()0.ParameterType. GetGenericTypeDefinition() == parameterType select m).FirstOrDefault().

You need to specify a concrete type using MethodInfo. MakeGenericMethod. However, I should point out, that getting the right type to invoke MakeGenericMethod on is not easy when you have an overloaded generic method.

Here is an example: var method = typeof(Foo) .GetMethods() . Where(x => x. Name == "Bar") .

Where(x => x. IsGenericMethod) . Where(x => x.

GetGenericArguments(). Length == 1) . Where(x => x.GetParameters().

Length == 1) . Where(x => x.GetParameters()0. ParameterType == typeof(Action).

MakeGenericType(x. GetGenericArguments()0) ) .Single(); method = method. MakeGenericMethod(new Type { typeof(int) }); Foo foo = new Foo(); method.

Invoke(foo, new Func { () => return 42; }).

That won't help. He can't get the open instance. – SLaks?

Dec 14 '10 at 19:43 Can you show me an example? – Anne Dec 14 '10 at 19:43 @SLaks: Yes, it is possible. I'll post an example.

– Jason Dec 14 '10 at 19:53 @SLaks, @Anne: Example posted. – Jason Dec 14 '10 at 20:02.

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