BN089-BS开发框架MVC4版/source20131023/Zephyr.Net/Zephyr.Utils/Reflection/PropertyExpressionParser.cs

(完整代码) 1KB

using System;
using System.Linq.Expressions;
using System.Reflection;

namespace Zephyr.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); }
		}
	}
}

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。