在.NET中创建DynamicType以实现接口,但使用基类中的成员实现

编程入门 行业动态 更新时间:2024-10-28 20:25:47
本文介绍了在.NET中创建DynamicType以实现接口,但使用基类中的成员实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试生成一个实现接口的动态类,但是其中一个或多个成员已经存在于基础中。我在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以实现接口,但使用基类中的成员实现

本文发布于:2023-11-16 13:34:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1604750.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类中   接口   成员   NET   DynamicType

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!