コード例 #1
0
        protected override string GetHeader()
        {
            string rv  = "";
            var    dis = typeof(ML).GetProperty(UtilsTool.GetPropertyName(LanguageFormat)).GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault() as DisplayAttribute;

            if (dis != null)
            {
                if (dis.ResourceType == null)
                {
                    rv = dis.Name;
                }
                else
                {
                    rv = Resources.Language.ResourceManager.GetString(dis.Name);
                }
            }
            return(rv);
        }
コード例 #2
0
        protected void ValidateDuplicateData(List <ValidationResult> rv)
        {
            if (this._duplicatedPropertiesCheck != null && this._duplicatedPropertiesCheck.Groups.Count > 0)
            {
                var baseExp              = DC.GetSet <TModel>().AsQueryable();
                var modelType            = typeof(TModel);
                ParameterExpression para = Expression.Parameter(modelType, "tm");
                foreach (var group in this._duplicatedPropertiesCheck.Groups)
                {
                    List <Expression> conditions = new List <Expression>();
                    //生成一个表达式,类似于 x=>x.ID != id
                    MemberExpression   idLeft     = Expression.Property(para, "ID");
                    ConstantExpression idRight    = Expression.Constant(Entity.ID);
                    BinaryExpression   idNotEqual = Expression.NotEqual(idLeft, idRight);
                    conditions.Add(idNotEqual);
                    List <PropertyInfo> props = new List <PropertyInfo>();
                    foreach (var field in group.Fields)
                    {
                        if (field.DirectFieldExp != null)
                        {
                            var    item         = field.DirectFieldExp;
                            string propertyName = UtilsTool.GetPropertyName(item);
                            var    prop         = UtilsTool.GetPropertyInfo(item);
                            props.Add(prop);
                            var func = item.Compile();
                            var val  = func.Invoke(this.Entity);

                            if (val == null)
                            {
                                continue;
                            }
                            if (val is string && val.ToString() == "")
                            {
                                var requiredAttrs = prop.GetCustomAttributes(typeof(RequiredAttribute), false);

                                if (requiredAttrs == null || requiredAttrs.Length == 0)
                                {
                                    continue;
                                }
                                else
                                {
                                    var requiredAtt = requiredAttrs[0] as RequiredAttribute;
                                    if (requiredAtt.AllowEmptyStrings == true)
                                    {
                                        continue;
                                    }
                                }
                            }
                            //生成一个表达式,类似于 x=>x.field == val
                            Expression left = Expression.Property(para, propertyName);
                            if (left.Type.IsGenericType && left.Type.GetGenericTypeDefinition() == typeof(Nullable <>))
                            {
                                left = Expression.Property(left, "Value");
                            }
                            if (left.Type == typeof(string))
                            {
                                left = Expression.Call(left, typeof(String).GetMethod("Trim", Type.EmptyTypes));
                            }
                            if (val is string)
                            {
                                val = val.ToString().Trim();
                            }
                            ConstantExpression right = Expression.Constant(val);
                            BinaryExpression   equal = Expression.Equal(left, right);
                            conditions.Add(equal);
                        }
                        else
                        {
                            dynamic    dField = field;
                            Expression exp    = dField.GetExpression(Entity, para);
                            if (exp != null)
                            {
                                conditions.Add(exp);
                            }
                            props.AddRange(dField.GetProperties());
                        }
                    }

                    int count = 0;
                    if (conditions.Count > 1)
                    {
                        Expression conExp = conditions[0];
                        for (int i = 1; i < conditions.Count; i++)
                        {
                            conExp = Expression.And(conExp, conditions[i]);
                        }

                        MethodCallExpression whereCallExpression = Expression.Call(
                            typeof(Queryable),
                            "Where",
                            new Type[] { modelType },
                            baseExp.Expression,
                            Expression.Lambda <Func <TModel, bool> >(conExp, new ParameterExpression[] { para }));
                        var result = baseExp.Provider.CreateQuery(whereCallExpression);
                        foreach (var res in result)
                        {
                            count++;
                        }
                    }
                    if (count > 0)
                    {
                        string AllName = "";
                        foreach (var prop in props)
                        {
                            var    dispAttrs = prop.GetCustomAttributes(typeof(DisplayAttribute), false);
                            string name      = prop.Name;
                            if (name == "LanguageCode")
                            {
                                continue;
                            }
                            if (dispAttrs != null && dispAttrs.Length > 0)
                            {
                                var attr = dispAttrs[0] as DisplayAttribute;
                                name = attr.Name;
                                if (attr.ResourceType != null)
                                {
                                    name = attr.ResourceType.GetProperty(name).GetValue(null, null).ToString();
                                }
                            }
                            AllName += name + ",";
                        }
                        if (AllName.EndsWith(","))
                        {
                            AllName = AllName.Remove(AllName.Length - 1);
                        }
                        if (props.Count == 1)
                        {
                            rv.Add(new ValidationResult(AllName + Resources.Language.字段重复, GetValidationFieldName(props[0])));
                        }
                        else if (props.Count > 1)
                        {
                            rv.Add(new ValidationResult(AllName + Resources.Language.组合字段重复, GetValidationFieldName(props[0])));
                        }
                        else
                        {
                            rv.Add(new ValidationResult(AllName + Resources.Language.组合字段重复));
                        }
                    }
                }
            }
        }
コード例 #3
0
        public virtual Type GetColumnType(Type ClassType)
        {
            Type rv = ClassType.GetProperty(UtilsTool.GetPropertyName(ColumnNameExp)).PropertyType;

            return(rv);
        }
コード例 #4
0
        public Expression GetExpression(T Entity, ParameterExpression para)
        {
            ParameterExpression midPara = Expression.Parameter(typeof(V), "tm2");
            var list = MiddleExp.Compile().Invoke(Entity);

            List <Expression> allExp = new List <Expression>();
            Expression        rv     = null;

            foreach (var li in list)
            {
                List <Expression> innerExp = new List <Expression>();
                bool needBreak             = false;
                foreach (var SubFieldExp in SubFieldExps)
                {
                    Expression left = Expression.Property(midPara, UtilsTool.GetPropertyName(SubFieldExp));
                    if (left.Type.IsGenericType && left.Type.GetGenericTypeDefinition() == typeof(Nullable <>))
                    {
                        left = Expression.Property(left, "Value");
                    }
                    if (left.Type == typeof(string))
                    {
                        left = Expression.Call(left, typeof(String).GetMethod("Trim", Type.EmptyTypes));
                    }
                    object vv = SubFieldExp.Compile().Invoke(li);
                    if (vv == null)
                    {
                        needBreak = true;
                        continue;
                    }
                    if (vv is string && vv.ToString() == "")
                    {
                        var requiredAttrs = li.GetType().GetProperty(UtilsTool.GetPropertyName(SubFieldExp)).GetCustomAttributes(typeof(RequiredAttribute), false);

                        if (requiredAttrs == null || requiredAttrs.Length == 0)
                        {
                            needBreak = true;
                            continue;
                        }
                        else
                        {
                            var requiredAtt = requiredAttrs[0] as RequiredAttribute;
                            if (requiredAtt.AllowEmptyStrings == true)
                            {
                                needBreak = true;
                                continue;
                            }
                        }
                    }

                    if (vv is string)
                    {
                        vv = vv.ToString().Trim();
                    }
                    ConstantExpression right = Expression.Constant(vv);
                    BinaryExpression   equal = Expression.Equal(left, right);
                    innerExp.Add(equal);
                }
                if (needBreak)
                {
                    continue;
                }
                Expression exp = null;
                if (innerExp.Count == 1)
                {
                    exp = innerExp[0];
                }
                if (innerExp.Count > 1)
                {
                    exp = Expression.And(innerExp[0], innerExp[1]);
                    for (int i = 2; i < innerExp.Count; i++)
                    {
                        exp = Expression.And(exp, innerExp[i]);
                    }
                }
                if (exp != null)
                {
                    var any = Expression.Call(
                        typeof(Enumerable),
                        "Any",
                        new Type[] { typeof(V) },
                        Expression.Property(para, UtilsTool.GetPropertyName(MiddleExp)),
                        Expression.Lambda <Func <V, bool> >(exp, new ParameterExpression[] { midPara }));
                    allExp.Add(any);
                }
            }
            if (allExp.Count == 1)
            {
                rv = allExp[0];
            }
            if (allExp.Count > 1)
            {
                rv = Expression.Or(allExp[0], allExp[1]);
                for (int i = 2; i < allExp.Count; i++)
                {
                    rv = Expression.Or(rv, allExp[i]);
                }
            }
            return(rv);
        }