title: How to use local variable as a type? Compiler says "it is a variable but is used like a type"
body: <p>At run-time, I don't know what type of variable <code>v1</code> is.
  For this reason, I wrote many <code>if else</code> statements:  </p>
  
  <pre><code>if (v1 is ShellProperty&lt;int?&gt;)
  {
      v2 = (v1 as ShellProperty&lt;int?&gt;).Value;
  }
  else if (v1 is ShellProperty&lt;uint?&gt;)
  {
      v2 = (v1 as ShellProperty&lt;uint?&gt;).Value;
  }
  else if (v1 is ShellProperty&lt;string&gt;)
  {
      v2 = (v1 as ShellProperty&lt;string&gt;).Value;
  }
  else if (v1 is ShellProperty&lt;object&gt;)
  {
      v2 = (v1 as ShellProperty&lt;object&gt;).Value;
  }    
  </code></pre>
  
  <p>The only difference is in <code>ShellProperty&lt;AnyType&gt;</code>. </p>
  
  <p>So instead of writing this with a lot of <code>if else</code> statements, I decided to use reflection to get the property type at run-time:</p>
  
  <pre><code> Type t1 = v1.GetType().GetProperty("Value").PropertyType;
   dynamic v2 = (v1 as ShellProperty&lt;t1&gt;).Value;
  </code></pre>
  
  <p>This code gets the <code>PropertyType</code> of <code>v1</code> and assigns it to the local variable <code>t1</code>, but after that, my compiler says that:</p>
  
  <blockquote>
    <p>t1 is a variable but is used like a type</p>
  </blockquote>
  
  <p>So it does not allow me to write <code>t1</code> inside <code>ShellProperty&lt;&gt;</code>. </p>
  
  <p>Please tell me how to solve this problem and how to get more compact code than what I have. Do I need to create a new class?       </p>