How to determine the Type of a null-Reference ?
-
Hi can anybody tell me how i determine the Type of a null-Reference ?
MyClass mc = null; mc.GetType() // throws a NullReferenceException typeof(mc) // work only with Classnames
I just want to know the Type of "mc" when it is null.
-
Hmmm, what about using the statement "is"?
if(mc is MyClass) {
}Does this work? Just a guess. Sebastian
-
Hi can anybody tell me how i determine the Type of a null-Reference ?
MyClass mc = null; mc.GetType() // throws a NullReferenceException typeof(mc) // work only with Classnames
I just want to know the Type of "mc" when it is null.
How can you possibly call a method in an object if it's null?
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
How can you possibly call a method in an object if it's null?
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001John Simmons / outlaw programmer wrote:
How can you possibly call a method in an object if it's null?
When it is a crazy extension method[^]
Recent blog posts: *Method hiding Vs. overriding *Microsoft Surface *SQL Server / Visual Studio install order My Blog
-
How can you possibly call a method in an object if it's null?
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Hi can anybody tell me how i determine the Type of a null-Reference ?
MyClass mc = null; mc.GetType() // throws a NullReferenceException typeof(mc) // work only with Classnames
I just want to know the Type of "mc" when it is null.
You can't. A null object doesn't have a type, that's why it throws an exception. Reflection is for viewing the runtime state of objects and during runtime a null object is null and has no type. If you need a Type object, you can use
typeof(MyClass)
. It's like asking if a pen is made from plastic or metal before it's been made. You can't. You can ask afterwards. But before, all you can do is look at the construction materials in the blueprint for a PlasticPen or a MetalPen (the classes), you don't know which it is until after you've created it.Simon
-
John Simmons / outlaw programmer wrote:
How can you possibly call a method in an object if it's null?
When it is a crazy extension method[^]
Recent blog posts: *Method hiding Vs. overriding *Microsoft Surface *SQL Server / Visual Studio install order My Blog
Interesting! I never considerd using extension methods for that sort of stuff, as you say in the blog, probably not the best idea though. The OP could do this then:
public static class Extensions
{
public static Type GetTypeIfNull(this MyClass instance)
{
return typeof(MyClass);
}
}Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
You can't. A null object doesn't have a type, that's why it throws an exception. Reflection is for viewing the runtime state of objects and during runtime a null object is null and has no type. If you need a Type object, you can use
typeof(MyClass)
. It's like asking if a pen is made from plastic or metal before it's been made. You can't. You can ask afterwards. But before, all you can do is look at the construction materials in the blueprint for a PlasticPen or a MetalPen (the classes), you don't know which it is until after you've created it.Simon
Correct. I gave you 5. As in the rating, not the gesture. I can give you 5 as in the gesture if you like though: *holds up palm*
Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
Alpha release: Entanglar: Transparant multiplayer framework for .Net games. -
Correct. I gave you 5. As in the rating, not the gesture. I can give you 5 as in the gesture if you like though: *holds up palm*
Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
Alpha release: Entanglar: Transparant multiplayer framework for .Net games.Thanks. *slaps raised palm* :laugh:
Simon
-
Thanks. *slaps raised palm* :laugh:
Simon
:laugh: Glad you didn't leave me hanging :D
Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
Alpha release: Entanglar: Transparant multiplayer framework for .Net games. -
Hi can anybody tell me how i determine the Type of a null-Reference ?
MyClass mc = null; mc.GetType() // throws a NullReferenceException typeof(mc) // work only with Classnames
I just want to know the Type of "mc" when it is null.
One thing no one really addressed is why you need to be able to do this? What is the problem you are trying to solve where you think you need to know the type of a null object?
Scott Dorman
Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]
Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai