zoukankan      html  css  js  c++  java
  • PropertyAccess类 Linq.Expressions 实现

    public class PropertyAccess
    {
    public const BindingFlags PropertyBinding = BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty
    //| BindingFlags.DeclaredOnly
    ;

    public readonly Func<object, object> GetValue;
    public readonly Action<object, object> SetValue;

    protected PropertyAccess(string name, bool nullable, PropertyInfo property)
    {
    // target: (object)((({TargetType})instance).{Property})
    // preparing parameter, object type
    ParameterExpression instance = Expression.Parameter(typeof(object), "instance");
    // ({TargetType})instance
    UnaryExpression instanceCast = Expression.Convert(instance, property.ReflectedType);
    if (property.CanRead)
    {
    // (({TargetType})instance).{Property}
    MemberExpression propertyAccess = Expression.Property(instanceCast, property);
    // (object)((({TargetType})instance).{Property})
    UnaryExpression castPropertyValue = Expression.Convert(propertyAccess, typeof(object));
    // Lambda expression
    GetValue = Expression.Lambda<Func<object, object>>(castPropertyValue, instance).Compile();
    }
    if (property.CanWrite)
    {
    ParameterExpression propertyValue
    = Expression.Parameter(typeof(object), "value");
    UnaryExpression propertyValueCast
    = Expression.Convert(propertyValue, property.PropertyType);
    MethodCallExpression callPropertyValue
    = Expression.Call(instanceCast, property.GetSetMethod(), propertyValueCast);
    SetValue
    = Expression.Lambda<Action<object, object>>(callPropertyValue, instance, propertyValue).Compile();
    }
    }

    public object ChangeType(object value)
    {
    return Convert.ChangeType(value, Nullable.GetUnderlyingType(base.EntityProperty.PropertyType) ?? base.EntityProperty.PropertyType);
    }
    }
  • 相关阅读:
    iOS学习之MVC,MVVM,MVP模式优缺点
    iOS学习之单例模式
    iOS学习之观察者模式
    iOS学习之设计模式
    iOS学习之SKTagView的使用
    iOS学习之cocoaPods
    iOS学习之git的使用
    iOS学习之block
    [学习笔记]一个实例理解Lingo的灵敏性分析
    爬虫实例(二)——爬取某宝评论
  • 原文地址:https://www.cnblogs.com/Googler/p/2000145.html
Copyright © 2011-2022 走看看