given
public Class Example
{
public static void Foo< T>(int ID){}
public static void Foo< T,U>(int ID){}
}
Questions:
- Is it correct to call this an "overloaded generic method"?
How can either method be specified in creation of a MethodInfo object?
Type exampleType = Type.GetType("fullyqualifiednameOfExample, namespaceOfExample"); MethodInfo mi = exampleType.GetMethod("Foo", BindingFlags.Public|BindingFlags.Static, null, new Type[] {typeof(Type), typeof(Type) }, null);
argument 4 causes the compiler much displeasure
Here is a Linq one-liner for what you need:
I make a little modification of your lambda query.
When the parameter type si generic you must make that like that :
I add
pi.ParameterType.GetGenericTypeDefinition()
and
In this way the method working very fine
Example :
With the modification of the method i can call this extension method
With my new Helper like this
The signature of my helper is
Here are the answers to your questions along with an example:
Yes, although there are two things really to be aware of here with generic methods, type inference and overload method resolution. Type inference occurs at compile time before the compiler tries to resolve overloaded method signatures. The compiler applies type inference logic to all generic methods that share the same name. In the overload resolution step, the compiler includes only those generic methods on which type inference succeeded. More here...
Please see the full example Console Application program code below that shows how several variants of the Foo method can be specified in creation of a MethodInfo object and then invoked using an Extension method:
Program.cs
Example.cs:
Extensions.cs:
I can't find a way of using GetMethod that would do what you want. But you can get all the methods and go through the list until you find the method that you want.
Remember you need to call MakeGenericMethod before you can actually use it.
Better:
Example attempt to get the correct overload of
System.Linq.Enumerable.Select
Another one liner to find the
MethodInfo
you want is to create a delegate;