Inconsistant null parameter handling in Reflection
From: Stefan Hong (stefan_at_mosp.net)
Date: 11/09/04
- Next message: Mansi: "Re: Exporting to Excel - how to copy/paste/insert rows?"
- Previous message: Sherif ElMetainy: "Re: remoting design"
- Next in thread: Jon Skeet [C# MVP]: "Re: Inconsistant null parameter handling in Reflection"
- Reply: Jon Skeet [C# MVP]: "Re: Inconsistant null parameter handling in Reflection"
- Reply: Robert Jordan: "Re: Inconsistant null parameter handling in Reflection"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 9 Nov 2004 14:09:22 +0800
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
- Next message: Mansi: "Re: Exporting to Excel - how to copy/paste/insert rows?"
- Previous message: Sherif ElMetainy: "Re: remoting design"
- Next in thread: Jon Skeet [C# MVP]: "Re: Inconsistant null parameter handling in Reflection"
- Reply: Jon Skeet [C# MVP]: "Re: Inconsistant null parameter handling in Reflection"
- Reply: Robert Jordan: "Re: Inconsistant null parameter handling in Reflection"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|