// 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.Text; using System.Collections.Generic; using System.Reflection; using System.CodeDom.Compiler; using Microsoft.CSharp; namespace E { public class Data { private string fix; private Dictionary map; public Data() : this("", null) { } public Data(string fix, Dictionary map) { this.fix = fix; this.map = map; } public virtual string Fix { get { return fix; } set { fix = value; } } public Dictionary Map { get { return map; } set { map = value; } } } public static class DataWrapper { private const string nstemp = @"using System; using E; namespace WrapperGen {{ {0} }}"; private const string clztemp = @"public class {0} : {1} {{ {2} }}"; private const string fldtemp = @"private Data<{0}> d;"; private const string contemp = @"public {0}(Data<{1}> d) {{ this.d = d; this.Fix = d.Fix; }}"; private const string proptemp1 = @"public override {0} {1} {{ get {{ return d.{1}; }} set {{ d.{1} = value; }} }}"; private const string proptemp2 = @"public {0} {1} {{ get {{ return d.Map[""{1}""]; }} set {{ d.Map[""{1}""] = value; }} }}"; private static Dictionary cache = new Dictionary(); private static Type RealWrap(Data tempobj, string clznam) { StringBuilder sb = new StringBuilder(); string t = typeof(T).Name; sb.Append(String.Format(fldtemp, t)); sb.Append(String.Format(contemp, clznam, t)); sb.Append(String.Format(proptemp1, "String", "Fix")); foreach(string key in tempobj.Map.Keys) { sb.Append(String.Format(proptemp2, t, key)); } string src = String.Format(nstemp, String.Format(clztemp, clznam, "Data<"+ t + ">", sb.ToString())); //Console.WriteLine(src); CodeDomProvider comp = new CSharpCodeProvider(); CompilerParameters param = new CompilerParameters(); param.GenerateInMemory = true; param.ReferencedAssemblies.Add("System.dll"); param.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location); CompilerResults res = comp.CompileAssemblyFromSource(param, src); //foreach(CompilerError ce in res.Errors) //{ // Console.WriteLine(ce); //} Assembly asm = res.CompiledAssembly; return asm.GetType("WrapperGen." + clznam); } public static Type Wrap(Data tempobj, string clznam) { if(!cache.ContainsKey(clznam)) { cache.Add(clznam, RealWrap(tempobj, clznam)); } return cache[clznam]; } public static Data Create(Type wt, Data o) { return (Data)wt.GetConstructor(new Type[] { typeof(Data) }).Invoke(new object[] { o }); } } public class Program { public static void Main(string[] args) { Data o = new Data("Foo", new Dictionary()); o.Map.Add("P1", "ABC"); o.Map.Add("P2", "DEF"); o.Map.Add("P3", "XYZ"); foreach(PropertyInfo pi in o.GetType().GetProperties()) { Console.WriteLine(pi.Name + " = " + pi.GetValue(o, null)); } Type fooclass = DataWrapper.Wrap(o, "FooClass"); Data wo = DataWrapper.Create(fooclass, o); foreach(PropertyInfo pi in wo.GetType().GetProperties()) { Console.WriteLine(pi.Name + " = " + pi.GetValue(wo, null)); } } } }