当前位置: 移动技术网 > IT编程>开发语言>.net > WebApi接口传参

WebApi接口传参

2018年08月17日  | 移动技术网IT编程  | 我要评论

正三轮摩托车价格,球球堂,股票期货个人投机精要

目前接口统一使用 [frombody]dictionary<string,string> req 来接收。

有时候,需要把从req字典提取多个字段赋值给 model,几个还好,几十个赋值就不好了。因此设计了使用泛型、反射帮助赋值。

设计不怎么通用,随着类型的增多,这个需要继续迭代。

public static a mapperthree<a>(dictionary<string, string> req)
        {
            a a = activator.createinstance<a>();
            try
            {               
                type typea = typeof(a);
                foreach (propertyinfo propertyinfo in typea.getproperties())
                {
                    if (req.containskey(propertyinfo.name))
                    {
                        // type t = ap.gettype();
                        string proname = propertyinfo.propertytype.name;
                        if (proname == "string")
                        {
                            propertyinfo.setvalue(a, req[propertyinfo.name]);
                        }
                        else if (proname == "int32")
                        {
                            propertyinfo.setvalue(a, convert.toint32(req[propertyinfo.name]));
                        }
                        else if (proname == "datetime")
                        {
                            propertyinfo.setvalue(a, convert.todatetime(req[propertyinfo.name]));
                        }
                        else if (propertyinfo.propertytype.isgenerictype && propertyinfo.propertytype.getgenerictypedefinition() == typeof(nullable<>))
                        {
                            
                                type[] typearray = propertyinfo.propertytype.getgenericarguments();
                                type basetype = typearray[0];
                                if (basetype.name== "int32")
                                {
                                    propertyinfo.setvalue(a, convert.toint32(req[propertyinfo.name]));
                                }
                                else if (basetype.name == "datetime")
                                {
                                    propertyinfo.setvalue(a, convert.todatetime(req[propertyinfo.name]));
                                }
                          
                        }
                        else
                        {
                            //非int类型 and string ,datetime类型 不做处理
                        }
                    }                  
                }
            }
            catch (exception ex)
            {
                throw ex;
            }
            return a;
        }
view code

在写这个方法时,有两个注意点

一:获取属性类型,官网 https://docs.microsoft.com/en-us/dotnet/api/system.reflection.propertyinfo.propertytype?redirectedfrom=msdn&view=netframework-4.7.2#system_reflection_propertyinfo_propertytype 

二:可空类型,拿出来的name都是一样的nullable`

也就是 nullable<system.datetime> 与 nullable<system.int> 的属性类型名字时一样的

         

解决办法:      从可空泛型参数里获取真实类型,下面代码,是根据实际需求设计,不具有通用性。

 

当然还有很多办法可以自动赋值,例如参数做对象绑定、参数为json字符串通过序列化、反序列化解决。各有优缺点,根据情况权衡吧。

推荐阅读:

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网