title: Passing class objects across different assembly versions
body: <p>The scenario goes like this -</p>
  
  <ul>
  <li>I have an assembly say 'MyAssembly'. An interface say 'IMyInterface' is defined in this assembly.</li>
  <li>In the same assembly i have one class(MyClass) with a method define in it as : </li>
  </ul>
  
  <blockquote>
    <p>public void MyMethod( IMyInterface object ) {  }</p>
  </blockquote>
  
  <ul>
  <li>Now in my project, i have created an interface with the same name and exact properties as exposed by the interface "IMyInterface" in 'MyAssembly'.</li>
  <li>I have a class which extends this interface(the one which i have created in my project) and i want to pass the object of that class as a parameter to the method "MyMethod" in different assembly using reflection.</li>
  </ul>
  
  <p><em>Issue is -</em></p>
  
  <ul>
  <li>When i try to invoke the method using reflection, i got this exception that <strong>"Cannot convert object to type IMyInterface"</strong>.</li>
  </ul>
  
  <p><em>Code -</em></p>
  
  <pre><code>Assembly myAssembly = Assembly.LoadFrom("MyAssembly");
  object classObject = myAssembly.CreateInstance("MyClass");
  Type classType = myAssembly.GetType("MyClass");
  MethodInfo myMethod = classType.GetMethod("MyMethod", BindingFlags.Instance);
  
  // Creating an object of class in the latest assembly and need to pass this
  // to method in assembly with different version.
  ClassExtendingMyInterface obj= new ClassExtendingMyInterface ();
  
  myMethod.Invoke(classObject, new object[] { obj});
  </code></pre>
  
  <p>If, i am getting it right, this is because object created is in different assembly and the parameter expected by the method is of its own assembly.</p>
  
  <p>Another approach i thought of was to create my own dynamic method in the class which will accept the object of my class.</p>
  
  <p>I have tried to google for and come across through <strong>Reflection.Emit</strong> or <strong>RunSharp</strong> to dynamically create your own class. But, we can use this only in our dynamically generated Assemblies but can't create dynamic methods or class in already existing assembly.</p>
  
  <p>I know generating assemblies at runtime is ain't good approach. But i can't think of anything now. Thanks for any help.</p>