Creating a DynamicType in .NET implementing an interface but using member implementations from a base class

后端 未结 2 973
甜味超标
甜味超标 2021-01-04 07:57

I am attempting to generate a dynamic class implementing an interface, but where one or more of the members already exists in the base. I compiled the following code in C# a

2条回答
  •  囚心锁ツ
    2021-01-04 08:44

    It looks like with TypeBuilder you will have to add a private pass-thru, just to make it happy (below). You could also try using the IKVM builder - almost identical API, but it might not have this limitation.

    using System;
    using System.Reflection;
    using System.Reflection.Emit;
    public class BaseClass
    {
        public string Bob
        {
            get { return "Bob"; }
        }
    }
    
    public interface IStuff
    {
        string Bob { get; }
    }
    static class Program
    {
        static void Main()
        {
            var name = new AssemblyName("foo");
            var asm = AppDomain.CurrentDomain.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run);
            var mod = asm.DefineDynamicModule("foo");
            var parent = typeof(BaseClass);
            var type = mod.DefineType("SubClass", parent.Attributes, parent);
            type.AddInterfaceImplementation(typeof(IStuff));
    
            var bob_get = type.DefineMethod("bob_get", MethodAttributes.Virtual | MethodAttributes.Private,
                typeof(string), Type.EmptyTypes);
            var il = bob_get.GetILGenerator();
            il.Emit(OpCodes.Ldarg_0);
            il.EmitCall(OpCodes.Callvirt, parent.GetProperty("Bob").GetGetMethod(), null);
            il.Emit(OpCodes.Ret);
            type.DefineMethodOverride(bob_get, typeof(IStuff).GetProperty("Bob").GetGetMethod());
            var final = type.CreateType();
            IStuff obj = (IStuff) Activator.CreateInstance(final);
            Console.WriteLine(obj.Bob);
        }
    }
    

提交回复
热议问题