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

 

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

上一篇:C# 获取本机可用端口


下一篇:C# 递归压缩图片