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);
    }
}

相关文章:

  • 2021-09-06
  • 2022-12-23
  • 2022-12-23
  • 2021-09-15
  • 2022-12-23
  • 2021-10-24
  • 2021-10-10
猜你喜欢
  • 2021-07-31
  • 2022-12-23
  • 2022-02-04
  • 2021-11-05
  • 2021-11-05
  • 2021-06-03
  • 2021-09-08
相关资源
相似解决方案