Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reflection: How to get a generic method? [duplicate]

Tags:

c#

reflection

Possible Duplicates:
How to use reflection to call generic Method?
Select Right Generic Method with Reflection

Hi there

Let's say I have two following two methods in a class:

public void MyMethod(object val) {} public void MyMethod<T>(T val) {} 

With reflection, I could get the first Method like this:

Type[] typeArray = new Type[1]; typeArray.SetValue(typeof(object), 1); var myMethod = myInstance.GetType().GetMethod("MyMethod", typeArray); 

But how can I get the second, generic method?

like image 629
sl3dg3 Avatar asked Mar 07 '11 10:03

sl3dg3


People also ask

How do I use reflection to call a generic method?

The first step to dynamically invoking a generic method with reflection is to use reflection to get access to the MethodInfo of the generic method. To do that simply do this: var methodInfo = typeof(ClassWithGenericMethod). GetMethod("MethodName");

Do generics use reflection?

Because the Common Language Runtime (CLR) has access to generic type information at run time, you can use reflection to obtain information about generic types in the same way as for non-generic types.

How do you invoke a generic method?

To call a generic method, you need to provide types that will be used during the method invocation. Those types can be passed as an instance of NType objects initialized with particular . NET types.


2 Answers

var myMethod = myInstance.GetType()                          .GetMethods()                          .Where(m => m.Name == "MyMethod")                          .Select(m => new {                                               Method = m,                                               Params = m.GetParameters(),                                               Args = m.GetGenericArguments()                                           })                          .Where(x => x.Params.Length == 1                                      && x.Args.Length == 1                                      && x.Params[0].ParameterType == x.Args[0])                          .Select(x => x.Method)                          .First(); 
like image 63
LukeH Avatar answered Oct 20 '22 05:10

LukeH


I would do it like this:

var methods = from m in typeof(MyClass).GetMethods()               where m.Name == "MyMethod"                  && m.IsGenericMethodDefinition                let typeParams = m.GetGenericArguments()               let normalParams = m.GetParameters()                where typeParams.Length == 1 && normalParams.Length == 1                  && typeParams.Single() == normalParams.Single().ParameterType                  && !typeParams.Single().GetGenericParameterConstraints().Any()                select m;  var myMethod = methods.Single(); 

We're looking for a method called "MyMethod" that is a generic method with a single type-parameter having no constraints, and one 'normal' parameter of the same type as the type-parameter.

Obviously, if you're not looking to be very precise, you can just do the bare minimum to disambiguate, such as:

var myMethod = typeof(MyClass)               .GetMethods()               .Single(m => m.Name == "MyMethod" && m.IsGenericMethodDefinition); 
like image 29
Ani Avatar answered Oct 20 '22 04:10

Ani