如何在C#中使用泛型参数调用泛型方法?

如何在C#中使用泛型参数调用泛型方法?,c#,generics,reflection,sqlite-net,C#,Generics,Reflection,Sqlite Net,我想知道如何使用C#中的反射调用以下方法: 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 #end

我想知道如何使用C#中的反射调用以下方法:

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
    {
    }
返回的参数数为1。。。这是错误的,但我不知道为什么系统返回给我这个号码

invoke方法因参数数目错误而失败


欢迎任何帮助

您使用GetWithChildren而不是GetAllWithChildren调用它。

您使用谓词的泛型参数使方法泛型。这意味着:

Expression
的泛型参数将是
Func
,它不是您要用以标记方法的实际类型。更新以下行:

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

这将为您提供来自
Func
T
类型。现在,它应该像预期的那样工作

以上更改基于这样一个假设,即您的谓词的类型为
Expression
。如果谓词是
Func
,则可以按如下方式获取参数类型:

Type parameterType  = predicate1.GetType().GetGenericArguments()[0];

您的泛型方法是扩展方法…请更改下面的代码并重试

MethodInfo methodInfo = typeof(**SQLiteConnection**).GetMethod("GetAllWithChildren", BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy);

泛型参数的数量为一(
T
)。参数的数量为3(
SQLiteConnection-conn
Expression-filter
bool-recursive
)。您可以通过调用
GetParameters
来获取参数。还要注意,
predicateType
将是
Expression
,这不是调用
MakeGenericMethod
时使用的正确类型。任何时候我看到
MakeGenericMethod
看到的都是HULK SMASH。微软需要修复这个疯狂的api
Type parameterType  = predicate1.GetType().GetGenericArguments()[0];
MethodInfo methodInfo = typeof(**SQLiteConnection**).GetMethod("GetAllWithChildren", BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy);