tags:

views:

359

answers:

1

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:

"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:

"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!

+1  A: 

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;