在.NET中创建DynamicType以实现接口,但使用基类中的成员实现 [英] Creating a DynamicType in .NET implementing an interface but using member implementations from a base class
问题描述
我正在尝试生成一个实现接口的动态类,但是其中一个或多个成员已经存在于基础中。我在C#中编译了以下代码,并在反射器中对其进行了检查,以查看C#编译器的作用。
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# and examined it in reflector to see what the C# compiler does.
class BaseClass
{
public string Bob
{
get { return "Bob"; }
}
}
interface IStuff
{
string Bob { get; }
}
class SubClass : BaseClass, IStuff
{
}
Reflector在子类中未显示任何实现。
Reflector does not show any implementation in SubClass.
.class private auto ansi beforefieldinit SubClass
extends Enterprise.Services.OperationalActions.Business.Filters.BaseClass
implements Enterprise.Services.OperationalActions.Business.Filters.IStuff
{
}
但是,如果我没有明确发出成员,则 TypeBuilder.CreateType()
会抛出一个 InvalidOperationException
指出该成员没有实现。所以我的问题是,如何告诉 TypeBuilder
接口成员应该从基础上接受它的实现?
But if I do not emit the member explicitly, TypeBuilder.CreateType()
throws an InvalidOperationException
stating that the member does not have an implementation. So my question is, how do I tell TypeBuilder
that an interface member should take it's implementation from the base?
推荐答案
使用 TypeBuilder
看起来,您必须添加一个私有的pass-thru,以便使其高兴(如下)。您还可以尝试使用 IKVM 构建器-几乎相同的API,但它可能没有此限制。
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);
}
}
这篇关于在.NET中创建DynamicType以实现接口,但使用基类中的成员实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!