Пример #1
0
        /// <summary>
        ///     合并NewExpression
        /// </summary>
        /// <param name="exps">要合并的NewExpression</param>
        public static Expression MergeNewExpression(params Expression[] exps)
        {
            // 获取表达式树中实体类型
            var parentType = new GetParamVisitor().Visit(exps[0]).Select(o => o.Type).First();

            // 取得所有PropertyInfo
            var lst = new GetMemberVisitor().Visit(exps).Select(o => (PropertyInfo)o.Member);

            // 构造函数参数类型
            var lstPropertyType = lst.Select(o => o.PropertyType).ToArray();

            // 根据取得的PropertyInfo列表,创建新类型
            var classType = DynamicsClassTypeCacheManger.Cache(null, lstPropertyType, parentType);
            // 获取新类型的构造函数
            var constructor = classType.GetConstructor(lstPropertyType);

            // 创建构造函数的参数表达式数组
            var constructorParam = BuildConstructorsParameters(classType, lst);

            return(Expression.New(constructor, constructorParam));
        }
Пример #2
0
        /// <summary>
        ///     初始化逻辑删除
        /// </summary>
        /// <param name="defineType">实体类型</param>
        public void Init(Type defineType)
        {
            Type fieldType;

            switch (FieldType)
            {
            case eumSortDeleteType.Number:
                fieldType = Value.GetType();
                break;

            case eumSortDeleteType.DateTime:
                fieldType = typeof(DateTime);
                Value     = DateTime.Now;
                break;

            default:
                fieldType = typeof(bool);
                break;
            }
            var dic = new Dictionary <string, Type>();

            dic[Name] = fieldType;

            // 如果当前类已包含该字段,则不创新派生类
            var sortDeleteClassType = defineType.GetProperty(Name) != null ? defineType : DynamicsClassTypeCacheManger.Cache(dic, defineType);
            var param  = Expression.Parameter(sortDeleteClassType, "o");
            var member = Expression.MakeMemberAccess(param, sortDeleteClassType.GetMember(Name)[0]);

            // 时间类型字段,则以参数形式(解析时动态转为当前时间)
            if (FieldType == eumSortDeleteType.DateTime)
            {
                AssignExpression     = Expression.Assign(member, Expression.Parameter(fieldType)); //DateTime.Now
                CondictionExpression = Expression.Equal(member, Expression.Convert(Expression.Constant(null), fieldType));
            }
            else
            {
                var valConstant = Expression.Constant(Value, member.Type);
                AssignExpression     = Expression.Assign(member, valConstant);
                CondictionExpression = Expression.NotEqual(member, valConstant);
            }
            CondictionExpression = Expression.Lambda(CondictionExpression, param);
        }