// 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 Castle.Core; using Castle.Core.Interceptor; using Castle.DynamicProxy; namespace E { // this is what we have public class C1 { public void M1() { Console.WriteLine("C1 M1"); } } public class C2 { public void M2() { Console.WriteLine("C1 M1"); } } // this is what we need to have to extend both C1 and C2 public class CX { public void MX() { Console.WriteLine("CX MX"); } } // helper stuff public interface I1 { void M1(); } public interface I2 { void M2(); } public interface IX { void MX(); } public interface IA : IX,I1,I2 { } public class C1D : C1, I1 { } public class C2D : C2, I2 { } public class CXD : CX, IX { } public static class ProxyHelper { public static TA CreateWrapper(this ProxyGenerator gen) where T1C : new() where T2C : new() { ProxyGenerationOptions opt = new ProxyGenerationOptions(); opt.AddMixinInstance(new T1C()); opt.AddMixinInstance(new T2C()); return (TA)gen.CreateClassProxy(typeof(TXC), new Type[] { typeof(TA) }, opt); } } // demo public class Program { public static void Main(string[] args) { ProxyGenerator gen = new ProxyGenerator(); IA o = gen.CreateWrapper(); o.M1(); o.M2(); o.MX(); } } }