title: .NET : How do you get the Type of a null object?
body: <p>I have a method with an out parameter that tries to do a type conversion. Basically:</p>
  
  <pre><code>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());
  }
  </code></pre>
  
  <p>The problem is that usually someone would call this like:</p>
  
  <pre><code>string output;
  GetParameterValue(output);
  </code></pre>
  
  <p>This will fail because of:</p>
  
  <pre><code>destination.GetType()
  </code></pre>
  
  <p>destination is null, so we can't call <code>.GetType()</code> on it. We also can not call:</p>
  
  <pre><code>typeof(destination)
  </code></pre>
  
  <p>because destination is a variable name not a type name.</p>
  
  <p>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.</p>
  
  <hr>
  
  <p>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 <code>DbParameter.Value</code> is of type object.</p>
  
  <p>What would be ideal would be for the developers to do something like:</p>
  
  <pre><code>string val = GetParameterValue("parameterName");
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>string val;
  GetParameterValue("parameterName", out val);
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>public T GetParameterValue&lt;T&gt;(string paramName)
  </code></pre>
  
  <p>So the developers can do:</p>
  
  <pre><code>string val = GetParameterValue&lt;string&gt;("parameterName");
  </code></pre>
  
  <p>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):</p>
  
  <pre><code>MyObj.SomeProp = GetParameterValue&lt;MyObj.SomeProp.GetType()&gt;("parameterName");
  </code></pre>
  
  <p>But again, if MyObj.SomeProp is null, that <code>.GetType()</code> call fails. The VM has to know the type of <code>MyObj.SomeProp</code>, even when its null, right? or else how would it catch cast exceptions?</p>
  
  <hr>
  
  <p>To partially solve my own problem, I can do:</p>
  
  <pre><code>MyObj.SomeProp = GetParameterValue&lt;typeof(MyObj).GetField("SomeProp").GetType()&gt;("parameterName");
  </code></pre>
  
  <p>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 (<code>MyObj.SomeProp</code>) and in the DB. There has to be a better way...</p>