I am taking in an interface, looping through the .GetEvents() return array and attempting to implement the event on my dynamic type. At the point when I try to call TypeBuilder.CreateType(), I am greeted with this lovely error:
我正在接受一个接口,循环遍历.GetEvents()返回数组并尝试在我的动态类型上实现该事件。在我尝试调用TypeBuilder.CreateType()时,我遇到了这个可爱的错误:
"Application method on type from assembly is overriding a method that has been overridden."
“来自程序集的类型的应用程序方法覆盖了已被覆盖的方法。”
If I comment out the typeBuilder.DefineMethodOverride calls that attempt to implement the interface methods, at the poin when I attempt to subscribe to the event I get the error:
如果我注释掉尝试实现接口方法的typeBuilder.DefineMethodOverride调用,当我尝试订阅事件时,我会得到错误:
"The method or operation is not implemented."
“该方法或操作未实施。”
Here is the method I have that is attempting to add the detected event to the emitted type. Just a quick note, I have other code defining the type and adding methods implementing those on the interface and all that code works fine. I had no problems until I attempted to add events into the mix.
这是我尝试将检测到的事件添加到发出的类型的方法。简单来说,我有其他代码定义类型并添加在接口上实现这些的方法,所有代码都可以正常工作。在我尝试将事件添加到混合中之前,我没有遇到任何问题。
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));
}
Google has been 0 help on this (searching for "is overriding a method that has been overridden" just returns a lot of Crystal Reports topics), and I have been struggling with this all morning. Any help would be greatly appreciated!
谷歌一直是这方面的帮助(搜索“覆盖已被覆盖的方法”只是返回了很多Crystal Reports主题),而且我整个上午一直在努力解决这个问题。任何帮助将不胜感激!
1 个解决方案
#1
Nevermind. When i loop through the method's of the interface to implement them I am accidentally implementing the add/remove also.
没关系。当我遍历界面的方法来实现它们时,我偶然也会实现添加/删除。
fixed it with this in my loop: if(method.IsSpecialName) continue;
在我的循环中用它修复它:if(method.IsSpecialName)继续;
#1
Nevermind. When i loop through the method's of the interface to implement them I am accidentally implementing the add/remove also.
没关系。当我遍历界面的方法来实现它们时,我偶然也会实现添加/删除。
fixed it with this in my loop: if(method.IsSpecialName) continue;
在我的循环中用它修复它:if(method.IsSpecialName)继续;