c# 自动将string字符串转成实体属性的类型

Convert.ChangeType()

看到.net webapi中有[FromUri]来接收参数  可以将自动参数转换成字段属性的类型

baidu 了许多文章 都在自己造轮子  突然发下微软提供了这个方法

        public static readonly BackArgs _args = new BackArgs();
        protected override void OnInit(EventArgs e)
        {
            foreach (System.Reflection.PropertyInfo item in typeof(BackArgs).GetProperties())
            {
                if (Request.Params[item.Name] != null)
                {
                    item.SetValue(_args, Convert.ChangeType(Request.Params[item.Name], Type.GetType(item.PropertyType.FullName)));
                }
            }
        }

猜你喜欢

转载自www.cnblogs.com/WeiYongZhi/p/11961563.html