zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    Shell 中 -n 条件判断的使用
    Linux shell 中(()) [] [[ ]] 的使用
    Linux 利用黑洞实现“取消在控制台输出日志”
    Hadoop DataNode 多目录磁盘扩展配置
    Linux 挂载硬盘
    css 对元素在文档中的排列的影响
    vue-route 基础
    javaScript 琐碎
    javaScript 事件流---冒泡 && 捕获
    页面优化---利用浏览器缓存
  • 原文地址:https://www.cnblogs.com/mrfangzheng/p/2136324.html
Copyright © 2011-2022 走看看