public List <PropertyInfo> GetProperties() { List <PropertyInfo> rv = new List <PropertyInfo>(); foreach (var subField in SubFieldExps) { var pro = UtilsTool.GetPropertyInfo(subField); if (pro != null) { rv.Add(pro); } } return(rv); }
protected virtual string GetHeader() { string rv = ""; var dis = UtilsTool.GetPropertyInfo(ColumnNameExp).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); }
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.组合字段重复)); } } } } }