• C#: 根据事件名称动态添加事件


    public static void BindCmdWithEventSrc(object eventSrc, string eventName, ICmd cmd)
    {
        Action act 
    = delegate
        {
        
    if (cmd != null)
        {
            cmd.Execute();
        }
        };

        EventInfo ei 
    = eventSrc.GetType().GetEvent(eventName);
        var handlerType 
    = ei.EventHandlerType;
        var eventParams 
    = handlerType.GetMethod("Invoke").GetParameters();
        
    //lambda: (object x0, EventArgs x1) => d()       
        var parameters = eventParams.Select(p => Expression.Parameter(p.ParameterType, "x"));
        
    // - assumes void method with no arguments but can be         
        
    //   changed to accomodate any supplied method       
        var body = Expression.Call(Expression.Constant(act), act.GetType().GetMethod("Invoke"));
        var lambda 
    = Expression.Lambda(body, parameters.ToArray());

        var del 
    = Delegate.CreateDelegate(handlerType, lambda.Compile(), "Invoke"false);
        ei.AddEventHandler(eventSrc, del);
    }

    public static void BindCmdWithEventSrc(object[,] bindings)
    {
        
    // bind control and command 
        for (int i = 0; i < bindings.GetLength(0); i++)
        {
        
    object eventSrc = bindings[i, 0];
        
    string eventName = bindings[i, 1as string;
        ICmd cmd 
    = bindings[i, 2as ICmd;

        BindCmdWithEventSrc(eventSrc, eventName, cmd);
        }
    }
  • 相关阅读:
    adobe acrobat 无效批注对象
    分享下今天研究的流量上限DDos攻击分析和解决方式
    【二】【HTML列表、表格与框架】
    大话计算机中的流水作业
    texinfo
    texindex
    texi2dvi
    tex, virtex, initex
    testprns printername [printcapname]
    testparm
  • 原文地址:https://www.cnblogs.com/mrfangzheng/p/2136324.html
Copyright © 2020-2023  润新知