开发者

Create a catch-all handler for all events and delegates in C#

I want to create a handler that can be used to handle any event or delegate. Specifically, I want to be able to write code like below:

class Invoker
{
    public object Invoke(object[] arg)
    {
        // generic handling code
    }
}

static void Main()
{
    var p = new Person();
    p.AddHandler("Event1", new Invoker().Invoke);
}

AddHandler is an extension method for object which receive an event name and a delegate of type Func<object[], object>. It should be able to do whatever magic to bind the event (e.g. Event1 in this case) to the provided delegate so that the delegate is invoked whenever the event is fired.

The signature of Event1 shouldn't matter because AddHandler should work with all types of events (and delegates).

I suspect this might involve some CIL generation to build a dynamic delegate matching the type of the specified event (e.g. Event1) and forwarding the call to the specified delegate (e.g. new Invoker().Invoke). I was able to build such a dynamic delegate, howev开发者_运维知识库er it could only forward to static methods, not instance methods because I couldn't find a way to push the bound instance of the to-be-invoked method into the CLR stack (i.e. the Invoker instance in the example). See the code provided below to see this issue clearly (see the line marked with ISSUE).

If anyone could point out a way to improve the dynamic generation code to capture bound object or better yet, suggest a simpler solution which doesn't need CIL then it is much appreciated.

public static void AddHandler(this object target, string fieldName,
    Func<object[], object> func)
{
    var eventInfo = target.GetType().GetEvent(fieldName);
    if (eventInfo != null)
    {
        Type delegateType = eventInfo.EventHandlerType;
        var dynamicHandler = BuildDynamicHandler(target.GetType(), delegateType, func);
        eventInfo.GetAddMethod().Invoke(target, new Object[] { dynamicHandler });
    }
}

public static Delegate BuildDynamicHandler(this Type delegateOwnerType, Type delegateType, 
    Func<object[], object> func)
{
    MethodInfo invokeMethod = delegateType.GetMethod("Invoke");
    Type returnType = invokeMethod.ReturnType;
    bool hasReturnType = returnType != Constants.VoidType;
    var paramTypes = invokeMethod.GetParameters().Select(p => p.ParameterType).ToArray();
    var dynamicMethod = new DynamicMethod("add_handler",
                                            hasReturnType ? returnType : null, paramTypes, delegateOwnerType);

    var il = new EmitHelper(dynamicMethod.GetILGenerator());
    if (paramTypes.Length == 0)
    {
        il.ldnull.end();
    }
    else
    {
        il.DeclareLocal(typeof(object[]));
        il.ldc_i4(paramTypes.Length);
        il.newarr(typeof(object));
        il.stloc_0.end();
        for (int i = 0; i < paramTypes.Length; i++)
        {
            il.ldloc_0
                .ldc_i4(i)
                .ldarg(i)
                .boxIfValueType(paramTypes[i])
                .stelem_ref.end();
        }
        il.ldloc_0.end();
    }

    /////// ******************  ISSUE: work for static method only
    il.call(func.Method); 
    if (hasReturnType)
    {
        il.unbox_any(returnType).ret();
    }
    else
    {
        il.pop.ret();
    }
    return dynamicMethod.CreateDelegate(delegateType);
}


Here's an implementation using expression trees:

    public static Delegate BuildDynamicHandle(Type delegateType, Func<object[], object> func)
    {
        var invokeMethod = delegateType.GetMethod("Invoke");
        var parms = invokeMethod.GetParameters().Select(parm => Expression.Parameter(parm.ParameterType, parm.Name)).ToArray();
        var instance = func.Target == null ? null : Expression.Constant(func.Target);
        var converted = parms.Select(parm => Expression.Convert(parm, typeof(object)));
        var call = Expression.Call(instance, func.Method, Expression.NewArrayInit(typeof(object), converted));
        var body =
            invokeMethod.ReturnType == typeof(void) ? (Expression)call : Expression.Convert(call, invokeMethod.ReturnType);
        var expr = Expression.Lambda(delegateType, body, parms);
        return expr.Compile();
    }


Have you looked at using expression trees (http://msdn.microsoft.com/en-us/library/bb397951.aspx)? They make it much easier to generate IL.


I worked out a solution. I blogged about it with the full code here, in case anyone interested in the pure CIL generation approach (which is not as elegant as kvb's approach).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜