Exemplo n.º 1
0
        public ObservableListInitializer(INotifyExpression <T> target, INotifyExpression <TElement> value, MethodInfo addMethod)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (addMethod == null)
            {
                throw new ArgumentNullException("addMethod");
            }

            Target = target;
            Value  = value;

            AddAction = ReflectionHelper.CreateDelegate <Action <T, TElement> >(addMethod);
            var removeMethod = ReflectionHelper.GetRemoveMethod(typeof(T), typeof(TElement));

            if (removeMethod == null)
            {
                throw new InvalidOperationException("Could not find appropriate Remove method for " + addMethod.Name);
            }
            if (removeMethod.ReturnType == typeof(void))
            {
                RemoveAction = ReflectionHelper.CreateDelegate <Action <T, TElement> >(removeMethod);
            }
            else if (removeMethod.ReturnType == typeof(bool))
            {
                var tempAction = ReflectionHelper.CreateDelegate <Func <T, TElement, bool> >(removeMethod);
                RemoveAction = (o, i) => tempAction(o, i);
            }
            else
            {
                throw new NotSupportedException();
            }
        }