Mega Code Archive

 
Categories / C# / Reflection
 

Creates a new instance of the specified type defined in the specified assembly file

using System; public interface ITest {     void Test(string greeting); } public class MarshallableExample : MarshalByRefObject, ITest {     static void Main()     {         string assemblyPath = Environment.CurrentDirectory + "\\" +typeof(MarshallableExample).Assembly.GetName().Name + ".exe";         AppDomain ad = AppDomain.CreateDomain("MyDomain");         System.Runtime.Remoting.ObjectHandle oh = ad.CreateInstanceFrom(assemblyPath, "MarshallableExample");         object obj = oh.Unwrap();         obj.GetType().InvokeMember("Test", System.Reflection.BindingFlags.InvokeMethod, Type.DefaultBinder, obj, new object[] { "Hello" });         ITest it = (ITest) obj;         it.Test("Hi");         MarshallableExample ex = (MarshallableExample) obj;         ex.Test("Goodbye");     }     public void Test(string greeting)     {         Console.WriteLine("{0} from '{1}'!", greeting,AppDomain.CurrentDomain.FriendlyName);     } }