// 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 = @" public class X { public override string ToString() { return ""X1""; } }"; string code2 = @" public class X { 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 = "X1.dll"; CompilerResults cr1 = c.CompileAssemblyFromSource(cp, code1); object x1 = Assembly.Load("X1").CreateInstance("X"); Console.WriteLine(x1); cp.OutputAssembly = "X2.dll"; CompilerResults cr2 = c.CompileAssemblyFromSource(cp, code2); object x2 = Assembly.Load("X2").CreateInstance("X"); Console.WriteLine(x2); } } }