示例#1
0
        internal static Func <object, IObservable <Unit> > CreatePropertyChangedFactory(this MemberExpression source)
        {
            var property = source.GetProperty();
            var inpc     = typeof(INotifyPropertyChanged).GetTypeInfo().IsAssignableFrom(property.DeclaringType.GetTypeInfo());

            return(t =>
            {
                if (t == null)
                {
                    return Observable <Unit> .Never;
                }
                if (!inpc)
                {
                    return Observable.Return(Unit.Default);
                }

                return Observable.FromEventPattern <PropertyChangedEventHandler, PropertyChangedEventArgs>
                (
                    handler => ((INotifyPropertyChanged)t).PropertyChanged += handler,
                    handler => ((INotifyPropertyChanged)t).PropertyChanged -= handler
                )
                .Where(args => args.EventArgs.PropertyName == property.Name)
                .Select(args => Unit.Default);
            });
        }
        internal static Func <object, object> CreateValueAccessor(this MemberExpression source)
        {
            // create an expression which accepts the parent and returns the child
            var property = source.GetProperty();
            var method   = property.GetMethod;

            if (method is null)
            {
                throw new ArgumentException("The property does not have a valid get method.", nameof(source));
            }

            if (source.Expression is null)
            {
                throw new ArgumentException("The source expression does not have a valid expression.", nameof(source));
            }

            // convert the parameter i.e. the declaring class to an object
            var parameter = Expression.Parameter(typeof(object));
            var converted = Expression.Convert(parameter, source.Expression.Type);

            // call the get value of the property and box it
            var propertyCall = Expression.Call(converted, method);
            var boxed        = Expression.Convert(propertyCall, typeof(object));
            var accessorExpr = Expression.Lambda <Func <object, object> >(boxed, parameter);

            return(accessorExpr.Compile());
        }
        internal static Func <object, IObservable <Unit> > CreatePropertyChangedFactory(this MemberExpression source)
        {
            var property = source.GetProperty();

            if (property.DeclaringType is null)
            {
                throw new ArgumentException("The property does not have a valid declaring type.", nameof(source));
            }

            var notifyPropertyChanged = typeof(INotifyPropertyChanged).GetTypeInfo().IsAssignableFrom(property.DeclaringType.GetTypeInfo());

            return(t =>
            {
                if (t is null)
                {
                    return Observable <Unit> .Never;
                }

                if (!notifyPropertyChanged)
                {
                    return Observable.Return(Unit.Default);
                }

                return Observable.FromEventPattern <PropertyChangedEventHandler, PropertyChangedEventArgs>(handler => ((INotifyPropertyChanged)t).PropertyChanged += handler, handler => ((INotifyPropertyChanged)t).PropertyChanged -= handler).Where(args => args.EventArgs.PropertyName == property.Name).Select(_ => Unit.Default);
            });
        }
示例#4
0
        internal static Func <object, object> CreateValueAccessor(this MemberExpression source)
        {
            //create an expression which accepts the parent and returns the child
            var property = source.GetProperty();
            var method   = property.GetMethod;

            //convert the parameter i.e. the declaring class to an object
            var parameter = Expression.Parameter(typeof(object));
            var converted = Expression.Convert(parameter, source.Expression.Type);

            //call the get value of the property and box it
            var propertyCall = Expression.Call(converted, method);
            var boxed        = Expression.Convert(propertyCall, typeof(object));
            var accessorExpr = Expression.Lambda <Func <object, object> >(boxed, parameter);

            var accessor = accessorExpr.Compile();

            return(accessor);
        }