实体类如下:  public class Peson
    {
        public string Name { get; set; }
        public string Sex { get; set; }
    }前台通过Jquery的中post或get方法发送数据到后台,格式:{"Name":"张三","Sex":"男"} 后台通过Request获取数据并添加到NameValueCollection中;
NameValueCollection paramValueCollection = new NameValueCollection();
paramValueCollection.Add(context.Request.QueryString);
paramValueCollection.Add(context.Request.Form);如何将NameValueCollection转换成实体类

解决方案 »

  1.   

    那您只能new 对象 手动赋值了
      

  2.   

            NameValueCollection paramValueCollection = new NameValueCollection();
            paramValueCollection.Add(Request.QueryString);
            paramValueCollection.Add(Request.Form);
            List<Peson> list = new List<Peson>();
            for (int i = 0; i < paramValueCollection.Count; i++)
                list.Add(new Peson() { Name = paramValueCollection.GetKey(i), Sex = paramValueCollection[i] });
      

  3.   

    两者之间转换,不用new对象!
      

  4.   


    namespace WebApplication1
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Type type = Type.GetType("WebApplication1.Peson");
                object obj = null;
                Type List = typeof(List<>);
                List = List.MakeGenericType(type);
                MethodInfo ListAdd = List.GetMethod("Add");
                object listObj = Activator.CreateInstance(List);
                for (int i = 0; i < Request.QueryString.Count; i++)
                {
                    obj = Activator.CreateInstance(type);
                    type.GetProperty("Key").SetValue(obj, Request.QueryString.GetKey(i), null);
                    type.GetProperty("Value").SetValue(obj, Request.QueryString[i], null);
                    ListAdd.Invoke(listObj, new object[] { obj });
                }
                int Count = (int)List.GetProperty("Count").GetValue(listObj, null);
                for (int i = 0; i < Count; i++)
                {
                    obj = List.InvokeMember("Item", BindingFlags.GetProperty, null, listObj, new object[] { i });
                    foreach (PropertyInfo p in type.GetProperties())
                        Response.Write(p.GetValue(obj, null) + "<br/>");
                }
            }
        }
        public class Peson
        {
            public string Key { get; set; }
            public string Value { get; set; }
        }
    }
      

  5.   


                            object o = Assembly.GetAssembly(para[i].ParameterType).CreateInstance(para[i].ParameterType.FullName);
                            PropertyInfo[] props = para[i].ParameterType.GetProperties();
                            foreach (PropertyInfo item in props)
                            {
                                if (paramValueCollection[item.Name.ToLower()] != null)
                                {
                                    item.SetValue(o, ChangeType(paramValueCollection[item.Name.ToLower()], item.PropertyType), null);
                                }
                            }
                            paramValueArray[i] = o;
                        }
    已经解决了