How to invoke Generic Method with Generic Parameter in C#?
Asked Answered
R

3

6

I would like to know how to use reflection in C# to call the following method :

public static List<T> GetAllWithChildren<T>
    (this SQLiteConnection conn, Expression<Func<T, bool>> filter = null, bool recursive = false) 
    where T
    #if USING_MVVMCROSS: new() #else : class #endif
    {
    }

My current code is:

MethodInfo methodInfo = typeof(ReadOperations).GetMethod("GetWithChildren", BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy);
Type predicateType = predicate.GetType();
MethodInfo genericMethod = methodInfo.MakeGenericMethod(predicateType);
Type[] genericArgumentsType = genericMethod.GetGenericArguments();
Debug.WriteLine("Arguments Number:" + genericArgumentsType.Count());
int count = 0;
foreach (Type ga in genericArgumentsType)
{
    Console.WriteLine(count++ + " " + ga.GetType());
}
Object[] genericArguments = { conn, predicate, true };
genericMethod.Invoke(conn, genericArguments);

The number of arguments returned is 1 ... that it's wrong but I don't know why the system return to me this number.

The invoke method fail with a wrong number of arguments.

Any help will be welcome!

Redeem answered 28/1, 2016 at 15:25 Comment(2)
The number of generic arguments is one (T). The number of parameters is 3 (SQLiteConnection conn, Expression<Func<T, bool>> filter and bool recursive). You can get the parameters by calling GetParameters.Carriole
Also note that predicateType will be Expression<Func<T, bool>> which is NOT the proper type to use when calling MakeGenericMethod.Carriole
E
2

You're using the Predicate's Generic argument to Make the method generic. which means:

Generic argument of Expression<Func<T, bool>> would be Func<T, bool> which is not actual type you're looking for to mark the method with. Update the following lines:

Type predicateType = predicate.GetType();
MethodInfo genericMethod = methodInfo.MakeGenericMethod(predicateType);

To

Type parameterType = predicate.Parameters[0].Type;
MethodInfo genericMethod = methodInfo.MakeGenericMethod(parameterType);

This will give you the type of T from Func<T,bool>. Now It should work as expected.

Above change are based on assumption that you're predicate is of type Expression<Func<T, bool>>. In case the predicate is Func<T, bool> then parameterType can be fetched like below:

Type parameterType  = predicate1.GetType().GetGenericArguments()[0];
Elate answered 28/1, 2016 at 16:7 Comment(1)
Any time i see MakeGenericMethod all i see is HULK SMASH. Microsoft needs to fix this insanity apiMackintosh
P
1

You're calling it with GetWithChildren, instead of GetAllWithChildren.

Proboscis answered 28/1, 2016 at 15:36 Comment(0)
C
0

your generic method is extension method....Change below code and try again

MethodInfo methodInfo = typeof(**SQLiteConnection**).GetMethod("GetAllWithChildren", BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy);
Convalesce answered 28/1, 2016 at 16:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.