// 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; namespace E { class MainClass { public static void Main(string[] args) { string code1 = @" using System; public class X : MarshalByRefObject { public override string ToString() { return ""X1""; } }"; string code2 = @" using System; public class X : MarshalByRefObject { public override string ToString() { return ""X2""; } }"; ICodeCompiler c = new CSharpCodeProvider().CreateCompiler(); CompilerParameters cp = new CompilerParameters(); cp.ReferencedAssemblies.Add("System.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); AppDomain.Unload(d1); CompilerResults cr2 = c.CompileAssemblyFromSource(cp, code2); AppDomain d2 = AppDomain.CreateDomain("Dom2"); object x2 = d2.CreateInstanceAndUnwrap("X", "X"); Console.WriteLine(x2); AppDomain.Unload(d2); } } }