c # string string will automatically turn into a type of entity properties

Convert.ChangeType()

There .net webapi see [FromUri] parameter may be received automatically converted into a parameter type field properties

baidu many articles are in themselves create the wheel suddenly under Microsoft provides this method

        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)));
                }
            }
        }

 

Guess you like

Origin www.cnblogs.com/WeiYongZhi/p/11961563.html