/// <summary> /// 从Form或Query获取参数值 /// </summary> /// <param name="request">请求数据</param> /// <param name="parameter">参数</param> /// <returns></returns> private object GetValueFromQueryForm(HttpRequest request, ApiParameter parameter) { var name = parameter.Name; var targetType = parameter.Type; if (targetType.IsClass == true && targetType.IsArray == false && targetType != typeof(string)) { return(this.QueryFormToClass(request, parameter)); } // 转换为数组 var values = request.GetValues(name); if (targetType.IsArray == true) { return(Converter.Cast(values, targetType)); } // 转换为简单类型 保留参数默认值 var value = values.FirstOrDefault(); if (value == null && parameter.Info.DefaultValue != DBNull.Value) { return(parameter.Info.DefaultValue); } return(Converter.Cast(value, targetType)); }
/// <summary> /// 生成模型 /// </summary> /// <param name="request">请求数据</param> /// <param name="parameter">参数</param> /// <returns></returns> public object BindModel(HttpRequest request, ParameterInfo parameter) { var name = parameter.Name; var targetType = parameter.ParameterType; if (targetType.IsClass == true && targetType.IsArray == false && targetType != typeof(string)) { return(ConvertToClass(request, parameter)); } // 转换为数组 var values = request.GetValues(name); if (targetType.IsArray == true) { return(Converter.Cast(values, targetType)); } // 转换为简单类型 保留参数默认值 var value = values.FirstOrDefault(); if (value == null && parameter.DefaultValue != DBNull.Value) { return(parameter.DefaultValue); } return(Converter.Cast(value, targetType)); }
/// <summary> /// 生成模型 /// </summary> /// <param name="request">请求数据</param> /// <param name="parameter">参数</param> /// <returns></returns> public object BindModel(HttpRequest request, ParameterInfo parameter) { var name = parameter.Name; var targetType = parameter.ParameterType; if (targetType.IsClass == true && targetType.IsArray == false && targetType != typeof(string)) { return ConvertToClass(request, parameter); } // 转换为数组 var values = request.GetValues(name); if (targetType.IsArray == true) { return Converter.Cast(values, targetType); } // 转换为简单类型 保留参数默认值 var value = values.FirstOrDefault(); if (value == null && parameter.DefaultValue != DBNull.Value) { return parameter.DefaultValue; } return Converter.Cast(value, targetType); }
/// <summary> /// 将请求Form或Query转换为参数模型 /// </summary> /// <param name="request">请求数据</param> /// <param name="parameter">参数</param> /// <returns></returns> private object GenerateModelFromQueryForm(HttpRequest request, ApiParameter parameter) { var name = parameter.Name; var targetType = parameter.Type; if (targetType.IsComplexClass() == true) { return(this.QueryFormToComplex(request, parameter)); } // 转换为数组 var values = request.GetValues(name); if (targetType.IsArrayOrList() == true) { return(Converter.Cast(values, targetType)); } // 转换为简单类型 保留参数默认值 var value = values.FirstOrDefault(); if (value == null && parameter.Info.DefaultValue != DBNull.Value) { return(parameter.Info.DefaultValue); } return(Converter.Cast(value, targetType)); }
/// <summary> /// 表单转换为类 /// </summary> /// <param name="request">请求数据</param> /// <param name="parameter">参数</param> /// <returns></returns> private object QueryFormToClass(HttpRequest request, ApiParameter parameter) { var targetType = parameter.Type; var instance = Activator.CreateInstance(targetType); var setters = ModelProperty.GetSetProperties(targetType); foreach (var setter in setters) { var value = request.GetValues(setter.Name).FirstOrDefault(); if (value != null) { var valueCast = Converter.Cast(value, setter.Info.PropertyType); setter.SetValue(instance, valueCast); } } return(instance); }
/// <summary> /// 生成模型 /// </summary> /// <param name="request">请求数据</param> /// <param name="parameter">参数</param> /// <returns></returns> public object BindModel(HttpRequest request, ParameterInfo parameter) { var name = parameter.Name; var type = parameter.ParameterType; if (type.IsClass == true && type.IsArray == false && type != typeof(string)) { return ConvertToClass(request, parameter); } var values = request.GetValues(name); if (type.IsArray == true) { return ConvertToArray(type, name, values); } return ConvertToSimple(type, name, values.FirstOrDefault(), parameter.DefaultValue); }
/// <summary> /// 生成模型 /// </summary> /// <param name="request">请求数据</param> /// <param name="parameter">参数</param> /// <returns></returns> public object BindModel(HttpRequest request, ParameterInfo parameter) { var name = parameter.Name; var type = parameter.ParameterType; if (type.IsClass == true && type.IsArray == false && type != typeof(string)) { return(ConvertToClass(request, parameter)); } var values = request.GetValues(name); if (type.IsArray == true) { return(ConvertToArray(type, name, values)); } return(ConvertToSimple(type, name, values.FirstOrDefault(), parameter.DefaultValue)); }
/// <summary> /// 转换为类 /// </summary> /// <param name="parameter">参数</param> /// <param name="request">请求数据</param> /// <returns></returns> public static object ConvertToClass(HttpRequest request, ParameterInfo parameter) { var targetType = parameter.ParameterType; if (targetType.IsByRef && targetType.IsInterface && targetType.IsAbstract) { throw new NotSupportedException("不支持的类型:" + targetType.Name); } var instance = Activator.CreateInstance(targetType); var setters = PropertySetter.GetPropertySetters(targetType); foreach (var setter in setters) { var value = request.GetValues(setter.Name).FirstOrDefault(); if (value != null) { var valueCast = Converter.Cast(value, setter.Type); setter.SetValue(instance, valueCast); } } return instance; }
/// <summary> /// 转换为类 /// </summary> /// <param name="parameter">参数</param> /// <param name="request">请求数据</param> /// <returns></returns> public static object ConvertToClass(HttpRequest request, ParameterInfo parameter) { var targetType = parameter.ParameterType; if (targetType.IsByRef && targetType.IsInterface && targetType.IsAbstract) { throw new NotSupportedException("不支持的类型:" + targetType.Name); } var instance = Activator.CreateInstance(targetType); var setters = BinderProperty.GetSetProperties(targetType); foreach (var setter in setters) { var value = request.GetValues(setter.Name).FirstOrDefault(); if (value != null) { var valueCast = Converter.Cast(value, setter.Info.PropertyType); setter.SetValue(instance, valueCast); } } return(instance); }