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!

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. Commented Jan 28, 2016 at 15:30
  • Also note that predicateType will be Expression<Func<T, bool>> which is NOT the proper type to use when calling MakeGenericMethod. Commented Jan 28, 2016 at 15:35

3 Answers 3

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];
Sign up to request clarification or add additional context in comments.

1 Comment

Any time i see MakeGenericMethod all i see is HULK SMASH. Microsoft needs to fix this insanity api
1

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

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.