Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET : How do you get the Type of a null object?

I have a method with an out parameter that tries to do a type conversion. Basically:

public void GetParameterValue(out object destination) {     object paramVal = "I want to return this. could be any type, not just string.";      destination = null; // default out param to null     destination = Convert.ChangeType(paramVal, destination.GetType()); } 

The problem is that usually someone would call this like:

string output; GetParameterValue(output); 

This will fail because of:

destination.GetType() 

destination is null, so we can't call .GetType() on it. We also can not call:

typeof(destination) 

because destination is a variable name not a type name.

So is there any way to get the type of an object that is set to null? I would think there would have to be a way to know what type a storage location is without it being assigned anything.


Just to give a bit more info, I am trying to make a utility method that will grab the output parameters of an Oracle stored procedure. The issue is that DbParameter.Value is of type object.

What would be ideal would be for the developers to do something like:

string val = GetParameterValue("parameterName"); 

The notable thing is that there is no casting of types. In practice, you don't know the lparam of the "equals", so I went with:

string val; GetParameterValue("parameterName", out val); 

And figured within the method, I would know the destination type of the output variable. I guess that was a bad assumption. As an alternative, I also wrote the method:

public T GetParameterValue<T>(string paramName) 

So the developers can do:

string val = GetParameterValue<string>("parameterName"); 

I find the explicit "string" declaration to be repetitive, especially since in practice, the destination if probably an object property and the oracle data type could change (think ORM):

MyObj.SomeProp = GetParameterValue<MyObj.SomeProp.GetType()>("parameterName"); 

But again, if MyObj.SomeProp is null, that .GetType() call fails. The VM has to know the type of MyObj.SomeProp, even when its null, right? or else how would it catch cast exceptions?


To partially solve my own problem, I can do:

MyObj.SomeProp = GetParameterValue<typeof(MyObj).GetField("SomeProp").GetType()>("parameterName"); 

The whole idea was to not have to explicitly use the Type in more than one place, so that if the data type changes, it only has to be changed in the destination object (MyObj.SomeProp) and in the DB. There has to be a better way...

like image 989
CodingWithSpike Avatar asked Oct 31 '08 18:10

CodingWithSpike


People also ask

What is the type of null in C#?

null (C# Reference) The null keyword is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables. Ordinary value types cannot be null, except for nullable value types.

How do you check what type an object is C#?

To determine whether an object is a specific type, you can use your language's type comparison keyword or construct. For example, you can use the TypeOf… Is construct in Visual Basic or the is keyword in C#. The GetType method is inherited by all types that derive from Object.

What type null is an object?

In JavaScript null is "nothing". It is supposed to be something that doesn't exist. Unfortunately, in JavaScript, the data type of null is an object. You can consider it a bug in JavaScript that typeof null is an object.

How do you handle a null object in C#?

IsNullOrEmpty() Method of C# If any string is not assigned any value, then it will have Null value. The symbol of assigning Null value is “ “or String. Empty(A constant for empty strings). This method will take a parameter that will be of System.


1 Answers

So is there any way to get the type of an object that is set to null? I would think there would have to be a way to know what type a storage location is without it being assigned anything.

Not necessarily. The best that you can say is that it is an object. A null reference does not point to any storage location, so there is no metadata from which it can make that determination.

The best that you could do is change it to be more generic, as in:

public void GetParameterValue<T>(out T destination) {     object paramVal = "Blah";     destination = default(T);     destination = Convert.ChangeType(paramVal, typeof(T)); } 

The type of T can be inferred, so you shouldn't need to give a type parameter to the method explicitly.

like image 147
Marcus Griep Avatar answered Sep 25 '22 07:09

Marcus Griep