调用Invoke会导致参数计数不匹配

jkj*_*000 3 c# reflection

我看像其他问题,但我有没有运气。我觉得我在围绕答案跳舞。

使用反射调用后,MethodInfo myMethod = MakeGenericMethod(Type.GetType(MyClass))MethodInfo在调试器中有了一个看起来像这样的对象:

myMethod --> Int32 Count[MyClass](System.Data.IDbConnection, ICriteria)
Run Code Online (Sandbox Code Playgroud)

...并且我尝试使用Invoke这样称呼它:

ICriteria myCriteria = new Criteria("some info here");

 //'connection' is an object of type System.Data.IDBConnection

int count = (int)myMethod.Invoke(connection, new object [] {myCriteria});
Run Code Online (Sandbox Code Playgroud)

...但是当我这样做时,我得到了一个参数计数不匹配的信息,而我却为之困惑。

是否因为可能是通用方法?还是事实Count是扩展方法connection

作为参考,调用我的方法的一种非反射性,直观的方法类似于 int count = connection.Count<MyRow>(new Criteria("some info here"));

xan*_*tos 6

该方法是扩展方法,因此它不是类的一部分。的第一个参数Invoke应该是null(它甚至可以为非null,但将被忽略)

int count = (int)myMethod.Invoke(null, new object [] { connection, myCriteria });
Run Code Online (Sandbox Code Playgroud)