// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Reflection; using System.CodeDom.Compiler; using Microsoft.CSharp; using E.Interface; namespace E.Demo { class MainClass { public static void Main(string[] args) { string code1 = @" using System; using E.Interface; public class X : MarshalByRefObject, IX { public override string ToString() { return ""X1""; } public string WhoAmI() { return ""I am X1""; } }"; string code2 = @" using System; using E.Interface; public class X : MarshalByRefObject, IX { public override string ToString() { return ""X2""; } public string WhoAmI() { return ""I am X2""; } }"; ICodeCompiler c = new CSharpCodeProvider().CreateCompiler(); CompilerParameters cp = new CompilerParameters(); cp.ReferencedAssemblies.Add("System.dll"); cp.ReferencedAssemblies.Add("IX.dll"); cp.GenerateInMemory = false; cp.OutputAssembly = "X.dll"; CompilerResults cr1 = c.CompileAssemblyFromSource(cp, code1); AppDomain d1 = AppDomain.CreateDomain("Dom1"); object x1 = d1.CreateInstanceAndUnwrap("X", "X"); Console.WriteLine(x1); Console.WriteLine(((IX)x1).WhoAmI()); AppDomain.Unload(d1); CompilerResults cr2 = c.CompileAssemblyFromSource(cp, code2); AppDomain d2 = AppDomain.CreateDomain("Dom2"); object x2 = d2.CreateInstanceAndUnwrap("X", "X"); Console.WriteLine(x2); Console.WriteLine(((IX)x2).WhoAmI()); AppDomain.Unload(d2); } } }