show more code? did you forget to inherit?if you use yourObject.GetType(), you should always get the subclass, for example, tryusing System;
using System.Reflection;public class a
{
public virtual void call()
{
Console.WriteLine("a::call()");
}
}public class b : a
{
public override void call()
{
Console.WriteLine("b::call()");
}
}class Test
{
static void Main()
{
b a1 = new b();
MethodInfo mi = a1.GetType().GetMethod("call");
mi.Invoke(a1,null); a a2 = new a();
mi = a2.GetType().GetMethod("call");
mi.Invoke(a2,null);
}
}
using System.Reflection;public class a
{
public virtual void call()
{
Console.WriteLine("a::call()");
}
}public class b : a
{
public override void call()
{
Console.WriteLine("b::call()");
}
}class Test
{
static void Main()
{
b a1 = new b();
MethodInfo mi = a1.GetType().GetMethod("call");
mi.Invoke(a1,null); a a2 = new a();
mi = a2.GetType().GetMethod("call");
mi.Invoke(a2,null);
}
}
b有call函数的其他overload版本或者同名的变量?就上面的程序没有问题。
忘了基类中还有一个虚方法。
重新看了一下,确定的错误信息是:
System.Reflection.AmbiguousMatchException:发现不明确的匹配。代码如下:
public class a
{
public virtual void call()
{
}
public virtual void call( int b )
{
}
}
public class b:a
{
public override void call()
{
}
}
MethodInfo miProc = tOper.GetMethod("call",xxx);// tOper 为类b的实例
xxx我已经试验过了BindingFlags.ExactBinding 和 BindingFlags.DeclaredOnly ,都不行
MethodInfo miproc = tOper.GetMethod("call", new Type[0]);取void call(int):
MethodInfo miproc = tOper.GetMethod("call", new Type[1] { int });