2

我希望能够通过传递事件的名称和依赖于客户端代码的操作来订阅任何对象的任何事件。我有以下代码

public static class EventSubscriber
{
    public static object Subscriber<TEventArgs>(
        this object obj,
        string eventName,
        Action handler,
        Func<TEventArgs, bool> canExecute)
    {
        var eventInfo = obj.GetType().
            GetEvent(eventName);

        if (eventInfo == null)
            throw new ArgumentException("Event name provided does not exist", nameof(eventName));

        var handlerArgs = eventInfo.EventHandlerType.
            GetMethod("Invoke").
            GetParameters()
            .Select(p => p.ParameterType).ToArray();



        var method = new DynamicMethod("method", typeof (void), handlerArgs);
        var generator = method.GetILGenerator(256);
        generator.EmitCall(OpCodes.Call, handler.Method, null);

        eventInfo.
            AddEventHandler(
                obj,
                method.CreateDelegate(eventInfo.EventHandlerType));
        return obj;
    }
}

上面代码的用法:

var Polygons = new ObservableCollection<Polygon>(myList);
Polygons.Subscriber<NotifyCollectionChangedEventArgs>
              ("CollectionChanged",
              () => MessageBox.Show("hello"),
              e => e.OldItems != null);

当事件触发时,它会导致 InvalidProgramException。我知道这是一个棘手的问题,我可以简单地使用 += 订阅,但有人能说出我的代码崩溃的原因吗?我想 ILGenerator.Emit 有问题,有什么建议吗?

4

1 回答 1

1

你忘了在结束时返回DynamicMethod

var method = new DynamicMethod("method", typeof (void), handlerArgs);
var generator = method.GetILGenerator(256);
generator.EmitCall(OpCodes.Call, handler.Method, null);
generator.Emit(OpCodes.Ret); //every method must have a return statement

编译器为 lambda 创建的类() => MessageBox.Show("hello")是私有的。[参考]

当您在类中使用public static方法时,public它会起作用。

var Polygons = new ObservableCollection<Polygon>(myList);
Polygons.Subscriber<NotifyCollectionChangedEventArgs>
    ("CollectionChanged",
    () => MessageBox.Show("hello"), //must in a public class and a public static method
    e => e.OldItems != null);
于 2016-12-03T23:55:18.380 回答