예제 #1
0
        private static void UpdateBinding(DependencyObject owner, EventBinding mbNew)
        {
            if (mbNew.DataContext != null)
            {
                object ctx = mbNew.DataContext;

                BindingFlags flg     = BindingFlags.Public | BindingFlags.Instance;
                Type         ctlType = owner.GetType();
                Type         ctxType = ctx.GetType();

                foreach (var binding in mbNew.Bindings)
                {
                    if (binding is CommandBinding)
                    {
                        CommandBinding cmdBinding = binding as CommandBinding;
                        EventInfo      evt        = ctlType.GetEvent(cmdBinding.EventName, flg);
                        if (evt == null)
                        {
                            continue;
                        }
                        PropertyInfo prop = ctxType.GetProperty(cmdBinding.TargetCommand, flg);
                        if (prop == null)
                        {
                            continue;
                        }

                        object cmd = prop.GetValue(ctx, null);                      // get the command

                        MethodInfo mthd = evt.EventHandlerType.GetMethod("Invoke"); // get the event handler

                        if (mthd != null)
                        {
                            var param = Ex.Parameter(typeof(EventArgs));
                            foreach (var p in mthd.GetParameters())
                            {
                                if (p.ParameterType != typeof(object))
                                {
                                    param = Ex.Parameter(p.ParameterType, p.Name);
                                    break;
                                }
                            }

                            var lam = Ex.Lambda(evt.EventHandlerType,
                                                Ex.Call(Ex.Constant(cmd), "Execute", null, param),
                                                Ex.Parameter(typeof(object)), param);

                            evt.AddEventHandler(owner, lam.Compile());
                        }
                    }
                }
            }
        }
예제 #2
0
        static Action <TObject, IEnumerable <object>, ILogger> BuildPropertySetter <TObject, TProperty>(string propertyName, Func <IEnumerable <object>, ILogger, TProperty> getter)
        {
            var obj    = Ex.Parameter(typeof(TObject));
            var values = Ex.Parameter(typeof(IEnumerable <object>));
            var logger = Ex.Parameter(typeof(ILogger));

            return(Ex
                   .Lambda <Action <TObject, IEnumerable <object>, ILogger> >(
                       Ex.Assign(
                           Ex.PropertyOrField(
                               obj,
                               propertyName),
                           Ex.Invoke(
                               Ex.Constant(getter),
                               values,
                               logger)),
                       obj,
                       values,
                       logger)
                   .Compile());
        }