/// <summary>
/// 获取请求参数字段
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T GetRequestParas<T>()
{ T t = (T)Activator.CreateInstance(typeof(T));
if (HttpContext.Current == null)
{
return t;
}
if (HttpContext.Current.Request.Form.Count < 0 && HttpContext.Current.Request.QueryString.Count < 0)
{
return t;
}
//获取所有为Public的字段和实例成员(如果有的话)
PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
//遍历每一个字段
foreach (PropertyInfo p in props)
{
var value = HttpContext.Current.Request[p.Name.ToLower()]; if (value != null)
{ var okvalue = ConvertTo(value, p.PropertyType);
if (okvalue == null)
{
okvalue = ConverUtil.toObject(value);
} p.SetValue(t, okvalue, null);
} }
return t;
} /// <summary>
/// 转换方法
/// </summary>
/// <param name="convertibleValue">待转换内容</param>
/// <param name="t">新的类型</param>
/// <returns>转换后的内容,如果转换不成功,返回null</returns>
public static object ConvertTo( IConvertible convertibleValue,Type t)
{
if (null == convertibleValue)
{
return null;
}
if (!t.IsGenericType)
{
return Convert.ChangeType(convertibleValue, t);
}
else
{
Type genericTypeDefinition = t.GetGenericTypeDefinition();
if (genericTypeDefinition == typeof(Nullable<>))
{
return ConverValue(convertibleValue.toString(), Nullable.GetUnderlyingType(t));
// Convert.ChangeType(convertibleValue, Nullable.GetUnderlyingType(t));
}
}
return null;
} /// <summary>
/// 执行TryParse方法进行转换
/// </summary>
/// <param name="value">待转换内容</param>
/// <param name="type">新的类型</param>
/// <returns>转换后的内容,如果转换不成功,返回null</returns>
public static object ConverValue(string value, Type type)
{
Object testValue = Activator.CreateInstance(type); object[] invokeArgs = new object[] { value, testValue };
Type[] types = new Type[2];
types[0] = typeof(string);
types[1] = type.MakeByRefType();
var isok = RunMethod("TryParse", types, invokeArgs, type);
bool outok = false;
bool.TryParse(isok.toString(), out outok);
if (outok)
{
return invokeArgs[1];
}
return null;
} /// <summary>
/// 执行反射的方法
/// </summary>
/// <param name="methodName">方法名</param>
/// <param name="types">参数类型</param>
/// <param name="invokeArgs">参数值</param>
/// <param name="invokeArgs">方法所在类的类型</param>
/// <returns></returns>
public static object RunMethod(string methodName, Type[] types, object[] invokeArgs, Type type)
{
Object Value = Activator.CreateInstance(type); MethodInfo mi = type.GetMethod(methodName, types);
if (mi == null)
{
return null;
}
Object returnValue = mi.Invoke(Value, invokeArgs);
return returnValue;
}

  写了大半天才写好,累死了。

方法的作用是自动匹配HTTP请求中对应实体参数名的数据,就是这样,其它的方法也可以单独用的,不过单独用的话最好写成扩展方法,直接点出来。

自动匹配HTTP请求中对应实体参数名的数据(性能不是最优)的更多相关文章

  1. springboot的restful风格获取请求中携带的参数

    http://localhost:8080/emp/1 有以上请求,我们controller要怎么获取请求中传递的参数1呢? 通过PathVariable注解,如下: @DeleteMapping(& ...

  2. js http 请求 多个相同参数名传值

    最近在用js和api做对接的时候需要传参数类似于 rights=a1&rights=a2 因为有相同的参数名,试过很多方法都被覆盖了. 最后终于发现可以通过rights=[a1,a2]的方式, ...

  3. Jquery中Ajax异步请求中的async参数的作用

    之前不知道这个参数的作用,上网找了前辈的博客,在此收录到自己的博客,希望能帮到更多的朋友: test.html <a href="javascript:void(0)" on ...

  4. JMeter怎么在get URL请求、POST请求中添加动态参数用于服务器段安全验证

    从前一个页面(含有服务器段返回给客户端的参数,用于在下一次请求时验证)中添加后置处理器中的Regular Expression Extractor,使用正则表达式对参数进行提取. 在用到这些变量时可以 ...

  5. postman 模拟请求中添加 header,post请求中传json参数

    1. GET 请求 2.Post 请求 (请求参数为Json,header中带有参数) 问题延伸 GET请求不能够 添加 Body 吗?[答案]

  6. linux curl http get 请求中带有中文参数或者特殊字符处理

    在使用c++去请求http服务的时候,使用的是著名的curl工具提供的类库 libcurl,但是在使用的过程中发现,如果请求的参数值带了空格或者是参数是中文,会导致响应的回调函数没有被执行,虽然cur ...

  7. LoadRunner---http请求中对中文参数的处理

    Loadrunner 做保险承保业务测试 1. 保险正常业务流程:保费计算--->保存--->申请核保--->核保--->缴费(出保单) 问题一描述 保费计算接口中,需要把车牌 ...

  8. Koa系框架(egg/cabloy)如何获取微信支付回调请求中的xml参数

    背景 在Koa系框架(如EggJS)中进行微信支付开发时,遇到一个问题:微信支付平台会发送一个回调请求,通知支付订单的处理结果.该请求传入的参数是xml格式,而Koa中间件koa-bodyparser ...

  9. ajax请求中包含中文参数

    对需要传递的中文参数先进行两次转码: 1.js文件中使用encodeURI()方法. var url = "Validate.jsp?id=" + encodeURI(encode ...

随机推荐

  1. BOOST 线程完全攻略 - 扩展 - 可被关闭的线程类

    本文假设读者已经基本了解boost线程库的使用方法. boost是个开源工程,线程这一块也在不断完善之中,到现在这个阶段,boost::thread仅仅实现了一个完美的技术框架,但是读者在实际使用中会 ...

  2. application windows are expected to have a root view controller错误

    产生这个提示的操作:在xcode4.6中创建一个名字为appTest空工程,create一个ios-application-empty application,直接编译运行 错误提示:虽然编译通过,也 ...

  3. VS2015预览版中的C#6.0 新功能(一)

    VS2015预览版中的C#6.0 新功能(二) VS2015预览版中的C#6.0 新功能(三) VS2015的预览版在11月12日发布了,下面让我们来看看C#都提供了哪些新的功能. 字符串添写(Str ...

  4. C# socket网络编程 基于TCP协议

    socket 服务器端: 1.创建socket Socket tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ...

  5. M - Candy Sharing Game

    Description A number of students sit in a circle facing their teacher in the center. Each student in ...

  6. .c_str()/atoi()/

    1. c_str是把string类型强制转换为const string 2. atpi(): Convert string to integer --Parses the C-string str i ...

  7. Image的Stride

    参看下面链接:http://msdn.microsoft.com/en-us/library/aa473780

  8. js各种验证文本框输入格式

    不能为空 <input onblur="if(this.value.replace(/^ +| +$/g,'')=='')alert('不能为空!')"> 只能输入英文 ...

  9. Python 之socket的应用

    本节主要讲解socket编程的有关知识点,顺便也会讲解一些其它的关联性知识: 一.概述(socket.socketserver): python对于socket编程,提供了两个模块,分别是socket ...

  10. python bottle框架(WEB开发、运维开发)教程

    教程目录 一:python基础(略,基础还是自己看书学吧) 二:bottle基础 python bottle web框架简介 python bottle 框架环境安装 python bottle 框架 ...