1

我正在接受一个接口,循环遍历 .GetEvents() 返回数组并尝试在我的动态类型上实现该事件。当我尝试调用 TypeBuilder.CreateType() 时,我遇到了这个可爱的错误:

“来自程序集的类型的应用程序方法正在覆盖已被覆盖的方法。”

如果我注释掉尝试实现接口方法的 typeBuilder.DefineMethodOverride 调用,那么当我尝试订阅事件时,我会收到错误消息:

“方法或操作未实现。”

这是我尝试将检测到的事件添加到发出类型的方法。快速说明一下,我还有其他代码定义了类型并添加了在接口上实现这些方法的方法,所有这些代码都可以正常工作。在我尝试将事件添加到组合中之前,我没有遇到任何问题。

protected static void AddEvent(EventInfo interfaceEvent, TypeBuilder proxyBuilder)
    {
        // Event methods attributes
        MethodAttributes eventMethodAttr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.NewSlot | MethodAttributes.Final | MethodAttributes.SpecialName;
        MethodImplAttributes eventMethodImpAtr = MethodImplAttributes.Managed | MethodImplAttributes.Synchronized;

        string qualifiedEventName = string.Format("{0}.{1}", typeof(T).Name, interfaceEvent.Name);
        string addMethodName = string.Format("add_{0}", interfaceEvent.Name);
        string remMethodName = string.Format("remove_{0}", interfaceEvent.Name);

        FieldBuilder eFieldBuilder = proxyBuilder.DefineField(qualifiedEventName,
            interfaceEvent.EventHandlerType, FieldAttributes.Public);

        EventBuilder eBuilder = proxyBuilder.DefineEvent(qualifiedEventName, EventAttributes.None, interfaceEvent.EventHandlerType);

        // ADD method
        MethodBuilder addMethodBuilder = proxyBuilder.DefineMethod(addMethodName,
            eventMethodAttr, null, new Type[] { interfaceEvent.EventHandlerType });

        addMethodBuilder.SetImplementationFlags(eventMethodImpAtr);

        // We need the 'Combine' method from the Delegate type
        MethodInfo combineInfo = typeof(Delegate).GetMethod("Combine", new Type[] { typeof(Delegate), typeof(Delegate) });

        // Code generation
        ILGenerator ilgen = addMethodBuilder.GetILGenerator();
        ilgen.Emit(OpCodes.Ldarg_0);
        ilgen.Emit(OpCodes.Ldarg_0);
        ilgen.Emit(OpCodes.Ldfld, eFieldBuilder);
        ilgen.Emit(OpCodes.Ldarg_1);                    
        ilgen.Emit(OpCodes.Call, combineInfo);            
        ilgen.Emit(OpCodes.Castclass, interfaceEvent.EventHandlerType);    
        ilgen.Emit(OpCodes.Stfld, eFieldBuilder);  
        ilgen.Emit(OpCodes.Ret);

        // REMOVE method
        MethodBuilder removeMethodBuilder = proxyBuilder.DefineMethod(remMethodName,
            eventMethodAttr, null, new Type[] { interfaceEvent.EventHandlerType });
        removeMethodBuilder.SetImplementationFlags(eventMethodImpAtr);

        MethodInfo removeInfo = typeof(Delegate).GetMethod("Remove", new Type[] { typeof(Delegate), typeof(Delegate) });

        // Code generation
        ilgen = removeMethodBuilder.GetILGenerator();
        ilgen.Emit(OpCodes.Ldarg_0);
        ilgen.Emit(OpCodes.Ldarg_0);
        ilgen.Emit(OpCodes.Ldfld, eFieldBuilder);    
        ilgen.Emit(OpCodes.Ldarg_1);                 
        ilgen.Emit(OpCodes.Call, removeInfo);            
        ilgen.Emit(OpCodes.Castclass, interfaceEvent.EventHandlerType);   
        ilgen.Emit(OpCodes.Stfld, eFieldBuilder);  
        ilgen.Emit(OpCodes.Ret);

        // Finally, setting the AddOn and RemoveOn methods for our event
        eBuilder.SetAddOnMethod(addMethodBuilder);
        eBuilder.SetRemoveOnMethod(removeMethodBuilder);

        // Implement the method from the interface
        proxyBuilder.DefineMethodOverride(addMethodBuilder, typeof(T).GetMethod("add_" + interfaceEvent.Name));

        // Implement the method from the interface
        proxyBuilder.DefineMethodOverride(removeMethodBuilder, typeof(T).GetMethod("remove_" + interfaceEvent.Name));

    }

谷歌对此的帮助为零(搜索“正在覆盖已被覆盖的方法”只会返回很多水晶报表主题),而我整个上午都在为此苦苦挣扎。任何帮助将不胜感激!

4

1 回答 1

1

没关系。当我遍历接口的方法来实现它们时,我也意外地实现了添加/删除。

在我的循环中用这个修复它:if(method.IsSpecialName) continue;

于 2009-04-15T16:07:00.593 回答