コード例 #1
0
        public static void Check <T>(T t)
        {
            var type       = t.GetType();
            var properties = PropertyCache.GetData(type);

            foreach (var property in properties)
            {
                object obj = property.GetValue(t, null);
                foreach (var attributeType in propertyAttributes)
                {
                    CheckResult(attributeType, property, obj);
                }
            }
        }
コード例 #2
0
        private TParam ConverRequestObject(IDictionary <string, string> requestString)
        {
            var targetType = typeof(TParam);

            var instance   = Activator.CreateInstance(targetType);
            var properties = PropertyCache.GetData(targetType);

            foreach (var propertyInfo in properties)
            {
                var key = requestString.Keys.FirstOrDefault(k => string.Equals(k, propertyInfo.Name, StringComparison.OrdinalIgnoreCase));
                if (key != null)
                {
                    //值类型
                    if (propertyInfo.PropertyType.IsValueType || propertyInfo.PropertyType == typeof(string))
                    {
                        IConvertible convertible;
                        if (propertyInfo.PropertyType.IsEnum)
                        {
                            var value = Enum.Parse(propertyInfo.PropertyType, requestString[key]);
                            convertible = value as IConvertible;
                        }
                        else
                        {
                            var value = requestString[key];
                            convertible = value as IConvertible;
                        }
                        if (convertible != null && typeof(IConvertible).IsAssignableFrom(propertyInfo.PropertyType))
                        {
                            var targetValue = convertible.ToType(propertyInfo.PropertyType, null);
                            propertyInfo.SetValue(instance, targetValue, null);
                            continue;
                        }
                    }
                    else
                    {
                        var targetValue = JsonConvert.DeserializeObject(requestString[key], propertyInfo.PropertyType);
                        propertyInfo.SetValue(instance, targetValue, null);
                        continue;
                    }
                }
            }

            return((TParam)instance);
        }