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, 1] as string;
ICmd cmd = bindings [I, 2] as ICmd;
BindCmdWithEventSrc (eventSrc, eventName, cmd );
}
}