jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Linq.Expressions;
using System.Reflection;
 
namespace HH.WMS.Utils
{
    public class PropertyExpressionParser<T>
    {
        private readonly object _item;
        private PropertyInfo _property;
 
        public PropertyExpressionParser(object item, Expression<Func<T, object>> propertyExpression)
        {
            _item = item;
            _property = GetProperty(propertyExpression);
        }
 
        private static PropertyInfo GetProperty(Expression<Func<T, object>> exp)
        {
            PropertyInfo result;
            if (exp.Body.NodeType == ExpressionType.Convert)
                result = ((MemberExpression) ((UnaryExpression) exp.Body).Operand).Member as PropertyInfo;
            else result = ((MemberExpression) exp.Body).Member as PropertyInfo;
 
            if (result != null)
                return typeof(T).GetProperty(result.Name);
 
            throw new ArgumentException(string.Format("Expression '{0}' does not refer to a property.", exp.ToString()));
        }
        
        public object Value
        {
            get { return ZReflection.GetPropertyValue(_item, _property); }
        }
 
        public string Name
        {
            get { return _property.Name; }
        }
 
        public Type Type
        {
            get { return ZReflection.GetPropertyType(_property); }
        }
    }
}