Example #1
0
        /// <summary>
        /// Based on a list of Expressions get a IObservedChanged for the value
        /// of the last property in the chain if possible.
        /// The Expressions are property chains. Eg Property1.Property2.Property3
        /// The method will make sure that each Expression can get a value along the way
        /// and get each property until each expression is evaluated.
        /// </summary>
        /// <param name="changeValues">A IObservedChanged for the value.</param>
        /// <param name="current">The object that starts the property chain.</param>
        /// <param name="expressionChain">A list of expressions which will point towards a property or field.</param>
        /// <returns>If the value was successfully retrieved or not.</returns>
        public static bool TryGetAllValuesForPropertyChain(out IObservedChange <object, object>[] changeValues, object current, IEnumerable <Expression> expressionChain)
        {
            int currentIndex = 0;
            var expressions  = expressionChain.ToList();

            changeValues = new IObservedChange <object, object> [expressions.Count()];

            foreach (Expression expression in expressions.SkipLast(1))
            {
                if (current == null)
                {
                    changeValues[currentIndex] = null;
                    return(false);
                }

                var sender = current;
                current = GetValueFetcherOrThrow(expression.GetMemberInfo())(current, expression.GetArgumentsArray());
                changeValues[currentIndex] = new ObservedChange <object, object>(sender, expression, current);
                currentIndex++;
            }

            if (current == null)
            {
                changeValues[currentIndex] = null;
                return(false);
            }

            Expression lastExpression = expressions.Last();

            changeValues[currentIndex] = new ObservedChange <object, object>(current, lastExpression, GetValueFetcherOrThrow(lastExpression.GetMemberInfo())(current, lastExpression.GetArgumentsArray()));

            return(true);
        }
Example #2
0
        /// <summary>
        /// Based on a list of Expressions get the value of the last property in the chain if possible.
        /// The Expressions are typically property chains. Eg Property1.Property2.Property3
        /// The method will make sure that each Expression can get a value along the way
        /// and get each property until each expression is evaluated.
        /// </summary>
        /// <param name="changeValue">A output value where to store the value if the value can be fetched.</param>
        /// <param name="current">The object that starts the property chain.</param>
        /// <param name="expressionChain">A list of expressions which will point towards a property or field.</param>
        /// <typeparam name="TValue">The type of the end value we are trying to get.</typeparam>
        /// <returns>If the value was successfully retrieved or not.</returns>
        public static bool TryGetValueForPropertyChain <TValue>(out TValue changeValue, object current, IEnumerable <Expression> expressionChain)
        {
            var expressions = expressionChain.ToList();

            foreach (Expression expression in expressions.SkipLast(1))
            {
                if (current == null)
                {
                    changeValue = default(TValue);
                    return(false);
                }

                current = GetValueFetcherOrThrow(expression.GetMemberInfo())(current, expression.GetArgumentsArray());
            }

            if (current == null)
            {
                changeValue = default(TValue);
                return(false);
            }

            Expression lastExpression = expressions.Last();

            changeValue = (TValue)GetValueFetcherOrThrow(lastExpression.GetMemberInfo())(current, lastExpression.GetArgumentsArray());
            return(true);
        }
Example #3
0
        public static bool TryGetValueForPropertyChain <TValue>(out TValue changeValue, object current, string[] propNames)
        {
            foreach (var propName in propNames.SkipLast(1))
            {
                if (current == null)
                {
                    changeValue = default(TValue);
                    return(false);
                }

                current = GetValueFetcherOrThrow(current.GetType(), propName)(current);
            }

            if (current == null)
            {
                changeValue = default(TValue);
                return(false);
            }

            changeValue = (TValue)GetValueFetcherOrThrow(current.GetType(), propNames.Last())(current);
            return(true);
        }
Example #4
0
        public static bool TryGetAllValuesForPropertyChain(out IObservedChange <object, object>[] changeValues, object current, string[] propNames)
        {
            int currentIndex = 0;

            changeValues = new IObservedChange <object, object> [propNames.Length];

            foreach (var propName in propNames.SkipLast(1))
            {
                if (current == null)
                {
                    changeValues[currentIndex] = null;
                    return(false);
                }

                var box = new ObservedChange <object, object> {
                    Sender = current, PropertyName = propName
                };
                current   = GetValueFetcherOrThrow(current.GetType(), propName)(current);
                box.Value = current;

                changeValues[currentIndex] = box;
                currentIndex++;
            }

            if (current == null)
            {
                changeValues[currentIndex] = null;
                return(false);
            }

            changeValues[currentIndex] = new ObservedChange <object, object> {
                Sender       = current,
                PropertyName = propNames.Last(),
                Value        = GetValueFetcherOrThrow(current.GetType(), propNames.Last())(current)
            };

            return(true);
        }