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!
T
). The number of parameters is 3 (SQLiteConnection conn
,Expression<Func<T, bool>> filter
andbool recursive
). You can get the parameters by callingGetParameters
. – CarriolepredicateType
will beExpression<Func<T, bool>>
which is NOT the proper type to use when callingMakeGenericMethod
. – Carriole