LINQ Expresstion Tree 表达式树

Expression trees represent code in a tree-like data structure, where each node is an expression, for example, a method call or a binary operation

MSDN的原文已经说明表达式树的作用就是以树状的数据结构来表示代码,树中的每个节点就是一个表达式,如方法调用或者二进制操作.表达式树的作用就是把代码以数据的形式来表达,
方便修改编辑,然后再转换为相应的代码来执行.

 

 

如何创建表达式树

我们在用委托这种类型来表示.net中的执行代码(方法) , 而做为代码的数据表示形式的表达式树我们是使用System.Linq.Expressions命名空间的Expression类型来表示.
例如委托Func<int, int>表示参数是int,返回值是int的实例方法或者静态方法的委托类型,那么Expression<Func<int, int>> 就是表示构建此类委托的数据结构,用于动态构建我们的方法.
  1. 直接使用lamda表达式来创建.
    static void Main(string[] args)
            {
                Expression<Func<int, int>> expresstion = x =>   x + 5;
                var expressionCompiled=expresstion.Compile();
                int input = 5;
                Console.WriteLine("input:{0},output:{1}",input, expressionCompiled(input));
                Console.Read();
            }
    image

    使用lamda表达式来创建表达式树有限制,就是lamda表达式只能是语句而不能是表达式块.
    例如:
    Expression<Func<int, int>> expresstion = x => { return x + 5; };
    编译器会报错:
    image
  2. 使用Expression类封装好的方法来创建表达式树.
                //construct the param expression
                var paramExpression= Expression.Parameter(typeof (int), "x");
                //and then the body expression
                var constantExpression=Expression.Constant(5, typeof (int));
                var bodyExpression= Expression.MakeBinary(ExpressionType.Add, paramExpression, constantExpression);
                //use the Lamda() method to construct the expression tree
                var func= Expression.Lambda<Func<int, int>>(bodyExpression, paramExpression).Compile();
                int input = 5;
                Console.WriteLine("input:{0},output:{1}",input, func(5));
    

    image

 

执行代码

表达式树只是代码的数据形式,不是执行代码,并不能直接执行.要转换为代码,需要调用Compile()方法编译,并返回委托对象.


实验一下:值对象的相等比较

DDD的值对象没有唯一的身份标识,直接来说就是没有唯一的ID(或者应该说即使ID不同的对象也可能看做是相同的对象,判断他们是否相同只能比较他们具体的一些属性,值类型不会单独存在,它应该作为实体类型或者其他值类型的属性依附存在.)
例如表示位置的值类型:

 public class Position
    {
        /// <summary>
        /// 经度
        /// </summary>
        [EqualKey]
        public decimal Lon { get; set; }
        /// <summary>
        /// 纬度
        /// </summary>
        [EqualKey]
        public decimal Lat { get; set; }
        /// <summary>
        /// 文字描述
        /// </summary>
        public string Description { get; set; }
    }

我们只需要根据经度跟纬度这两个属性是否相等来判断Positon值是否相等,经度跟纬度这两个关键的属性会使用自定义的特征EqualKeyAttribute来标识.
所有的值类型都继承自类型抽象基类ValueObject,且基类的Equal方法被重写,相等比较的逻辑在虚方法中通过反射寻找每个值类型的关键属性来构建表达式树,
最后编译成特定的Func委托实现.

/// <summary>
    /// 值类型基类
    /// </summary>
    public abstract class ValueObject<T> where T:ValueObject<T>
    {

        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            if (this.GetType() != obj.GetType()) return false;
            return BuildValueComparer()((T)this, (T)obj);            
        }

        /// <summary>
        /// 建立值比较委托(子类可重写)
        /// </summary>
        /// <returns></returns>
        protected virtual Func<T, T, bool> BuildValueComparer()
        {
            return ValueObjectComparerFactory.BuildComparer<T>();            
        }
        

        /// <summary>
        /// 重载==
        /// </summary>
        /// <returns></returns>
        public static bool operator ==(ValueObject<T> source,ValueObject<T> compare) 
        {
            if (source == null)
            {
                return compare == null;
            }
            return source.Equals(compare);
        }
        /// <summary>
        /// 重载!=
        /// </summary>
        /// <returns></returns>
        public static bool operator !=(ValueObject<T> source, ValueObject<T> compare)
        {
            return !(source == compare);
        }

    }

    [AttributeUsage(AttributeTargets.Property)]
    public class EqualKeyAttribute : Attribute
    {

    }

    internal interface IValueObjectComparerBuilder<T>
    {
        Func<T, T, bool> BuildComparer<T>();
    }

    static class ValueObjectComparerFactory
    {
        private static Dictionary<Type, object> cache = new Dictionary<Type, object>();
        private static object syncObject = new object();
        internal static Func<T, T, bool> BuildComparer<T>()
        {
            var objectType = typeof (T);
            if (!cache.ContainsKey(objectType))
            {
                lock (syncObject)
                {
                    if (!cache.ContainsKey(objectType))
                    {
                        IValueObjectComparerBuilder<T> builder = new ExpressionTreeComparerBuilder<T>();
                        cache.Add(objectType, builder.BuildComparer<T>());
                    }
                }
            }
            return cache[objectType] as Func<T, T, bool>;
        }
    }

    internal class ExpressionTreeComparerBuilder <T>: IValueObjectComparerBuilder<T>
    {
        public Func<T, T, bool> BuildComparer<T>()
        {
            Expression<Func<T, T, bool>> expression = null;
            Expression finalExpression = null;
            Type objectType = typeof (T);
            //parameter a,b
            var paramA = Expression.Parameter(objectType, "a");
            var paramB = Expression.Parameter(objectType, "b");
            //var typeParamA = Expression.Convert(paramA, objectType);
            //var typeParamB = Expression.Convert(paramB, objectType);


            foreach (var propertyInfo in objectType.GetProperties())
            {
                if (propertyInfo.GetCustomAttribute<EqualKeyAttribute>(false) != null)
                {
                    var propertyExpressionA = Expression.Property(paramA, propertyInfo);
                    var propertyExpressionB = Expression.Property(paramB, propertyInfo);
                    var equalExpression = Expression.MakeBinary(ExpressionType.Equal, propertyExpressionA, propertyExpressionB);
                    if (finalExpression == null)
                        finalExpression = equalExpression;
                    else
                        finalExpression = Expression.MakeBinary(ExpressionType.AndAlso, finalExpression, equalExpression);
                }
            }
            if (finalExpression != null)
                return Expression.Lambda<Func<T, T, bool>>(finalExpression, paramA, paramB).Compile();
            else
                return (a, b) => a.Equals(b);
        }
    }

测试一下:
static void Main(string[] args)
        {          
            Position pos1 = new Position
            {
                Lat = 100,
                Lon = 100,
                Description = "佛山市"
            };

            Position pos2 = new Position
            {
                Lat = 100,
                Lon = 100,
                Description = "顺德区"
            };

            Position pos3 = new Position
            {
                Lat = 111,
                Lon = 111,
                Description = "佛山市"
            };

            Console.WriteLine("{0}=={1} Result=>{2}",pos1.ToString(),pos2.ToString(), pos1.Equals(pos2).ToString());
            Console.WriteLine("{0}=={1} Result=>{2}", pos1.ToString(), pos3.ToString(), pos1.Equals(pos3).ToString());
            Console.Read();
        
image

 

To be continued………………


 


参考:
https://blogs.msdn.microsoft.com/charlie/2008/01/31/expression-tree-basics/

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/expression-trees/index

转载于:https://www.cnblogs.com/DotnetFocus/p/8457406.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值