S
Stefan Hong
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.WriteLine("Hello(string)");
}
public void Hello(object obj) {
Console.WriteLine("Hello(object)");
}
}
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("Hello", new Type[] {
typeof(void) });
method.invoke(test, 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.WriteLine("Hello(string)");
}
public void Hello(object obj) {
Console.WriteLine("Hello(object)");
}
}
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("Hello", new Type[] {
typeof(void) });
method.invoke(test, 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