Example #1
0
        public static PropertyPath <TValue> Create <TValue>(Expression <Func <TValue> > propertyExpression)
        {
            var          path          = PropertyPathVisitor.GetPath(propertyExpression);
            var          propertyInfos = path.Cast <PropertyInfo>().ToArray();
            var          parts         = new PathProperty[propertyInfos.Length];
            PathProperty previous      = null;

            for (int i = 0; i < propertyInfos.Length; i++)
            {
                var propertyInfo = propertyInfos[i];
                var item         = new PathProperty(previous, propertyInfo);
                parts[i] = item;
                previous = item;
            }

            var constants = ConstantVisitor.GetConstants(propertyExpression);

            if (!constants.Any())
            {
                throw new ArgumentException("Expression contains no constants", nameof(propertyExpression));
            }

            //valuePath.Source = source;
            var source       = constants.Last().Value;
            var propertyPath = new PropertyPath(parts);

            return(new PropertyPath <TValue>(propertyPath, source));
        }
Example #2
0
        /// <summary>
        /// The get path.
        /// </summary>
        /// <param name="expression">
        /// The expression.
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        /// <returns>
        /// The <see cref="MemberExpression[]"/>.
        /// </returns>
        public static IReadOnlyList <MemberInfo> GetPath <T>(Expression <Func <T> > expression)
        {
            var visitor = new PropertyPathVisitor(expression);

            visitor.Visit(expression);
            visitor.path.Reverse();
            return(visitor.path);
        }
Example #3
0
        public static PropertyPath <TSource, TValue> Create <TSource, TValue>(Expression <Func <TSource, TValue> > propertyExpression)
        {
            var          path          = PropertyPathVisitor.GetPath(propertyExpression);
            var          propertyInfos = path.Cast <PropertyInfo>().ToArray();
            var          parts         = new PathProperty[propertyInfos.Length];
            PathProperty previous      = null;

            for (int i = 0; i < propertyInfos.Length; i++)
            {
                var propertyInfo = propertyInfos[i];
                var item         = new PathProperty(previous, propertyInfo);
                parts[i] = item;
                previous = item;
            }

            return(new PropertyPath <TSource, TValue>(new PropertyPath(parts)));
        }