title: How can I cast an Interface as its type in c#?
body: <p>I have a property that returns an interface. During debugging I can break on what was returned and while it is the interface, Visual Studio is smart enough to know the derived type that it actually is. I assume it's using reflection or something. I'm not sure. My question is, can I have that same info available to me at runtime so I can create a variable of the appropriate type and cast the interface as that? Here is what I am saying:</p>
  
  <pre><code>IPreDisplay preDisplay = cb.PreDisplay;
  </code></pre>
  
  <p>If preDisplay is a RedPreDisplay I would like to be able to code</p>
  
  <pre><code>RedPreDisplay tmp = preDisplay as RedPreDisplay;
  </code></pre>
  
  <p>Or if preDisplay were a GreenPreDisplay...</p>
  
  <pre><code>GreenPreDisplay tmp = preDisplay as GreenPreDisplay;
  </code></pre>
  
  <p>etc...
  I would like to avoid a messy switch statement if possible, and If I could use generics that would be great.</p>
  
  <p>If you have any advice or examples of how I can do this, please share.</p>