jeudi 28 janvier 2016

How to invoke Generic Method with Generic Parameter in C#?

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!





Aucun commentaire:

Enregistrer un commentaire