Hi,
It seems that the way reflection resolves methods is not quite the same as
default CLR. For example this simple class:
class Test {
public void Hello(string name) {
Console.WriteLi ne("Hello(strin g)");
}
public void Hello(object obj) {
Console.WriteLi ne("Hello(objec t)");
}
}
If I invoke the method normally using null parameter:
Test test = new Test();
test.Hello(null ); // shows Hello(string)
But if I only know the input parameter (which is null) and the name of the
method, and trying to use reflection to resolve the method to invoke, it
becomes:
Test test = new Test();
MethodInfo method = test.GetType(). GetMethod("Hell o", new Type[] {
typeof(void) });
method.invoke(t est, new object[] { null }); // shows Hello(object)
The result changed. It must because of the typeof(void) thing, but I can't
say typeof(string) because the input parameter is null. What should I do to
get the same result?
Thanks,
Stefan
It seems that the way reflection resolves methods is not quite the same as
default CLR. For example this simple class:
class Test {
public void Hello(string name) {
Console.WriteLi ne("Hello(strin g)");
}
public void Hello(object obj) {
Console.WriteLi ne("Hello(objec t)");
}
}
If I invoke the method normally using null parameter:
Test test = new Test();
test.Hello(null ); // shows Hello(string)
But if I only know the input parameter (which is null) and the name of the
method, and trying to use reflection to resolve the method to invoke, it
becomes:
Test test = new Test();
MethodInfo method = test.GetType(). GetMethod("Hell o", new Type[] {
typeof(void) });
method.invoke(t est, new object[] { null }); // shows Hello(object)
The result changed. It must because of the typeof(void) thing, but I can't
say typeof(string) because the input parameter is null. What should I do to
get the same result?
Thanks,
Stefan
Comment