Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A general delegate type for handling any event

I have a function that receives two parameters - an object, and an EventInfo structure defining an event on that object. I need to block that function until the specified event fires. The problem I have is, how do I add a delegate to the specified event, when the type of handler could be anything? Note that I don't care about the parameters of that resulting event call, I just need to catch the fact it is raised.

I have already tried using EventInfo.AddEventHandler to add a really general delegate type (EventHandler), but to no avail. I have also tried the same, but using Activator to create an instance of the type specified in the EventInfo.EventHandlerType property, but no joy.

Alternatively, if anyone has a way to do similar, given an object, and the name of an event on that object, then that would work also.

I am using C# and .NET 2.0.

Cheers

like image 471
Alistair Evans Avatar asked Oct 27 '22 01:10

Alistair Evans


1 Answers

A hint to the solution can be use of MethodBuilder class. Using it you can generate a method at runtime that fits the delegate the EventInfo expects.

Example based on it (Many optimizations can be done but it works for most cases):

namespace AutoEventListener
{
    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Reflection.Emit;

    public class EventExample
    {
        public static event EventHandler MyEvent;

        public void Test()
        {
            bool called;
            var eventInfo = GetType().GetEvent("MyEvent");
            EventFireNotifier.GenerateHandlerNorifier(eventInfo,
                callbackEventInfo =>
                    {
                        called = true;
                    });

            MyEvent(null, null);;
        }
    }

    public class EventFireNotifier
    {
        static private readonly Dictionary<int, EventInfo> eventsMap = new Dictionary<int, EventInfo>();
        static private readonly Dictionary<int, Action<EventInfo>> actionsMap = new Dictionary<int, Action<EventInfo>>();
        static private int lastIndexUsed;
        public static MethodInfo GenerateHandlerNorifier(EventInfo eventInfo, Action<EventInfo> action)
        {
            MethodInfo method = eventInfo.EventHandlerType.GetMethod("Invoke");
            AppDomain myDomain = AppDomain.CurrentDomain;
            var asmName = new AssemblyName(){Name = "HandlersDynamicAssembly"};

            AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly(
                asmName,
                AssemblyBuilderAccess.RunAndSave);

            ModuleBuilder myModule = myAsmBuilder.DefineDynamicModule("DynamicHandlersModule");

            TypeBuilder typeBuilder = myModule.DefineType("EventHandlersContainer", TypeAttributes.Public);

            var eventIndex = ++lastIndexUsed;
            eventsMap.Add(eventIndex, eventInfo);
            actionsMap.Add(eventIndex, action);

            var handlerName = "HandlerNotifierMethod" + eventIndex;

            var parameterTypes = method.GetParameters().Select(info => info.ParameterType).ToArray();
            AddMethodDynamically(typeBuilder, handlerName, parameterTypes, method.ReturnType, eventIndex);

            Type type = typeBuilder.CreateType();

            MethodInfo notifier = type.GetMethod(handlerName);

            var handlerDelegate = Delegate.CreateDelegate(eventInfo.EventHandlerType, notifier);

            eventInfo.AddEventHandler(null, handlerDelegate);
            return notifier;
        }

        public static void AddMethodDynamically(TypeBuilder myTypeBld, string mthdName, Type[] mthdParams, Type returnType, int eventIndex)
        {
            MethodBuilder myMthdBld = myTypeBld.DefineMethod(
                                                 mthdName,
                                                 MethodAttributes.Public |
                                                 MethodAttributes.Static,
                                                 returnType,
                                                 mthdParams);

            ILGenerator generator = myMthdBld.GetILGenerator();

            generator.Emit(OpCodes.Ldc_I4, eventIndex);
            generator.EmitCall(OpCodes.Call, typeof(EventFireNotifier).GetMethod("Notifier"), null);
            generator.Emit(OpCodes.Ret);
        }

        public static void Notifier(int eventIndex)
        {
            var eventInfo = eventsMap[eventIndex];
            actionsMap[eventIndex].DynamicInvoke(eventInfo);
        }
    }
}

The class EventFireNotifier register for EventInfo an Action which is called when the event is fired.

I hope it helps.

like image 67
Elisha Avatar answered Nov 11 '22 08:11

Elisha