コード例 #1
0
        public static VerigyResultData Verigy <T>(T entity)
        {
            Type                 t          = entity.GetType();
            var                  properties = t.GetProperties();
            string               columnMappingName;
            VerigyResultData     result            = new VerigyResultData();
            List <AttributeData> attributeDataList = new List <AttributeData>();
            AttributeData        attributeData;
            object               value;

            foreach (var propertity in properties)
            {
                value = propertity.GetValue(entity, null);
                if (value == null)
                {
                }
                columnMappingName = propertity.Name;
                var displayAttributes = propertity.GetCustomAttributes(typeof(DisplayAttribute), false);
                if (displayAttributes.GetLength(0) > 0)
                {
                    columnMappingName = ((DisplayAttribute)displayAttributes[0]).Name;
                }
                attributeData = new AttributeData();
                attributeData.AttributeCode = propertity.Name;
                attributeData.AttributeName = columnMappingName;

                //验证基础属性
                VerigyObjectAttribute(propertity, value, attributeData, attributeDataList);

                //验证正则表达式
                VerigyRegularExpressionAttribute(propertity, value, attributeData, attributeDataList);

                //验证其它属性
                VerigyAttributeQueue(propertity, value, attributeData, attributeDataList);
            }
            if (attributeDataList.Count > 0)
            {
                attributeDataList = attributeDataList.GroupBy(p => new { p.AttributeCode, p.AttributeName })
                                    .Select(p => new AttributeData()
                {
                    AttributeCode = p.Key.AttributeCode,
                    AttributeName = p.Key.AttributeName,
                    ErrorList     = p.SelectMany(q => q.ErrorList).ToList()
                }).ToList();
                result.InfoList.AddRange(attributeDataList);
            }
            if (result.InfoList.Count == 0)
            {
                result.Status = true;
            }
            return(result);
        }
コード例 #2
0
        public static VerigyResultData Verigy <T>(T entity, bool isReturnMessage)
        {
            StringBuilder    sb     = new StringBuilder();
            VerigyResultData result = Verigy(entity);

            if (!result.Status && isReturnMessage)
            {
                foreach (var errorInfo in result.InfoList)
                {
                    sb.AppendFormat(@"{1}{2}!",
                                    errorInfo.AttributeCode,
                                    errorInfo.AttributeName,
                                    errorInfo.ErrorList.Aggregate((a, b) => a + "," + b));
                }
                result.InfoString = sb.ToString();
            }
            return(result);
        }