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的实例方法或者静态方法的委托类型,那么Expression> 就是表示构建此类委托的数据结构,用于动态构建我们的方法.
  1. 直接使用lamda表达式来创建.
    static void Main(string[] args)
            {
                Expression> 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> 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>(bodyExpression, paramExpression).Compile();
                int input = 5;
                Console.WriteLine("input:{0},output:{1}",input, func(5));
    

    LINQ Expresstion Tree 表达式树_第1张图片

 

执行代码

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


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

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

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

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

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

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

        /// 
        /// 建立值比较委托(子类可重写)
        /// 
        /// 
        protected virtual Func BuildValueComparer()
        {
            return ValueObjectComparerFactory.BuildComparer();            
        }
        

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

    }

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

    }

    internal interface IValueObjectComparerBuilder
    {
        Func BuildComparer();
    }

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

    internal class ExpressionTreeComparerBuilder : IValueObjectComparerBuilder
    {
        public Func BuildComparer()
        {
            Expression> 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(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>(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();
        
LINQ Expresstion Tree 表达式树_第2张图片

 

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

你可能感兴趣的:(LINQ Expresstion Tree 表达式树)