// 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 src = @"public class X { public override string ToString() { return ""I am a X""; } };"; ICodeCompiler comp = (new CSharpCodeProvider()).CreateCompiler(); CompilerParameters param = new CompilerParameters(); param.GenerateInMemory = true; param.ReferencedAssemblies.Add("System.dll"); CompilerResults res = comp.CompileAssemblyFromSource(param, src); Assembly asm = res.CompiledAssembly; object o = asm.CreateInstance("X"); Console.WriteLine(o); } } }