C# 关于 &符号字符串转对象

 string respDataJson = "A=&B=2&C=asdfasdf";

 

 

 string respDataJson = "A=&B=2&C=asdfasdf";
           var dddddd= StringToObject<ResponseModel>(respDataJson);

 

 

 

public static T StringToObject<T>(this string str) where T : new()
        {
            Type typeData = typeof(T);
            T entity = new T();
          
            System.Reflection.PropertyInfo[] proArray = typeData.GetProperties();

            var pairs = str.Split('&');
            foreach (var p in pairs)
            {
                var items = p.Split('=');
                if (items.Length == 2)
                {
                    System.Reflection.PropertyInfo pro= proArray.Where(x => x.Name.ToUpper() == items[0].ToUpper()).FirstOrDefault();
                    if (pro != null)
                    {
                        pro.SetValue(entity, items[1], null);
                    }
                }
            }
            return entity;
        }

 

 

 

 public class ResponseModel
        {
            public string A { get; set; }
            public string B { get; set; }
            public string C { get; set; }
        }

 

posted @ 2021-05-08 10:36  LuoCore  阅读(319)  评论(0编辑  收藏  举报