using System; using System.Linq.Expressions; using System.Reflection; namespace HH.WMS.Utils { public class PropertyExpressionParser { private readonly object _item; private PropertyInfo _property; public PropertyExpressionParser(object item, Expression> propertyExpression) { _item = item; _property = GetProperty(propertyExpression); } private static PropertyInfo GetProperty(Expression> 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); } } } }