title: Passing a Variable as a Type WITHOUT using Reflection: 'myvar' is a variable but is used like a type
body: <p>I've got a simple method that deserializes an xml string to a type that is specified.  </p>
  
  <pre><code>public static class TextUtility
  {
      public static T Deserialize&lt;T&gt;(string xml)
      {
          ...
      }
  }
  </code></pre>
  
  <p>That's all good.  Where I'm having trouble is in calling this method.  The process looks like this:</p>
  
  <pre><code>public interface IMessageReceived
  {
      string MessageBodyAsXML { get; set; }
      string MessageType { get; set; }
  }
  
  public class MessageReceived : IMessageReceived
  {...}
  
  List&lt;MessageReceived&gt; messageList = new List&lt;MessageReceived&gt;();
  </code></pre>
  
  <p>Then, in the worker class that is processing the messages, we loop over all the messages received.  Inside that loop, we loop over a list of potential message types.</p>
  
  <pre><code>foreach (MessageReceived message in messageList)
  {
      foreach (var processor in processors)
      {
          if (processor.TypeToProcess.Name == message.MessageType)
          {
               // Now Create an instance of 'processor' from message.MessageBodyAsXML
          }
      }
  }
  </code></pre>
  
  <p>I tries this:</p>
  
  <pre><code>object messageObject = TextUtility.DeserializeMessage&lt;object&gt;(message.MessageBodyAsXML);
  </code></pre>
  
  <p>And it kinda works, but the generic 'object' causes problems down stream.  I need to be more tightly coupled. I can do this:</p>
  
  <pre><code>Type type = processor.GetType();
  </code></pre>
  
  <p>And that <em>does</em> populate 'type' with the correct type. But then obviously I can't do this:</p>
  
  <pre><code>type messageObject = TextUtility.DeserializeMessage&lt;type&gt;(message.MessageBodyAsXML);
  </code></pre>
  
  <p>I am trying hard to avoid a long if/then for each type. Open to ideas.  Thanks!</p>