title: How to convert a class to dynamic parent interface?
body: <p>I have a generic interface and some corresponding code(All the codes below are in the <code>ClassLibrary1</code> namespace): </p>
  
  <pre><code>interface IProcessor&lt;T&gt;
  {
      void process(T message);
  }
  
  public class Second{/*Store some message*/}
  public class First{/*Store some message*/}
  
  public enum MessageType
  {
      First,
      Second
  }
  </code></pre>
  
  <p>and a <code>Processor</code> class </p>
  
  <pre><code>public class Processor : IProcessor&lt;First&gt;,IProcessor&lt;Second&gt;
  {
      public void process(First message){/*process the "First" type message*/}
      public void process(Second message){/*process the "Second" type message*/}
  }
  </code></pre>
  
  <p>So I had several overload version of <code>process</code> method.</p>
  
  <p>I write a <code>dispatcher</code> class:</p>
  
  <pre><code>public partial class Dispatcher
  {
      private const string NS_BASE = "ClassLibrary1";
      private static Processor processor = new Processor();
      static partial void Dispatch(MessageType msgType, Object message);
      static partial void Dispatch_cached(MessageType msgType, Object message);
      // basic declaration above, implemntation follows
      // message parameter corresponds with msgType.The method signature is compulsory and can't be modified()
  }
  </code></pre>
  
  <p>I first used direct reflection:</p>
  
  <pre><code>public partial class Dispatcher
  {    
      static partial void Dispatch(MessageType msgType,Object message)
      {
          Type messageType = Type.GetType(NS_BASE + "." + msgType.ToString());
          Type processorType = typeof(Processor);
          Type[] typeArr = new Type[] { messageType };
          Object[] msgArr = new Object[] { message };
          MethodInfo processMethod = processorType.GetMethod("process", typeArr);
          processMethod.Invoke(processor, msgArr);
      }
  }
  </code></pre>
  
  <p>And improve the performance using cache:</p>
  
  <pre><code>public partial class Dispatcher
  {    
      private static Dictionary&lt;String, MethodInfo&gt; typeDictionary = initDict();
  
      private static Dictionary&lt;String, MethodInfo&gt; initDict()
      {
          string[] names = Enum.GetNames(typeof(MessageType));
          return names.ToDictionary(msgTypeStr =&gt; msgTypeStr, msgTypeStr =&gt;
          {
              Type messageType = Type.GetType(NS_BASE + "." + msgTypeStr.ToString());
              Type processorType = typeof(Processor);
              Type[] typeArr = new Type[] { messageType };
              return processorType.GetMethod("process", typeArr);
          });
      }
  
      static partial void Dispatch_cached(MessageType msgType, Object message)
      {
          if (typeDictionary.TryGetValue(msgType.ToString(), out MethodInfo processorMethod))
          {
              Object[] msgArr = new Object[] { message };
              processorMethod.Invoke(processor, msgArr);
          }
      }
  }
  </code></pre>
  
  <p>The performance actually improved 5~6 times according to my test, but i also see possible reflection alternatives such as expression,emit or DLR. <strong>So do these alternative solutions fit in the scene and will they improve performance?</strong></p>
  
  <p>These are problems that I actually face.'How to convert a class to dynamic parent interface?' is just a qustion I come up with when trying to solve it using DLR.</p>
  
  <pre><code>static void Dispatch_dynamic(MessageType msgType, dynamic message)
  {
      processor.process(message);// I think this line will raise a runtime error
      //can I convert processor to IFoo&lt;First/Second&gt; type and call the processor_converted.process(message)?
  }
  </code></pre>