public object ToObject(Type t) { if (t == null) { return(null); } object instance = Activator.CreateInstance(t); foreach (FieldInfo fi in t.GetFields()) { var name = fi.Name;//获得属性的名字,后面就可以根据名字判断来进行些自己想要的操作 if (((IDictionary)this).Contains(name)) { IJsonWrapper wrapper = this[name]; if (wrapper == null) { fi.SetValue(instance, null); } else if (wrapper.IsObject) { fi.SetValue(instance, wrapper.ToObject(fi.FieldType)); } else if (wrapper.IsArray) { IList wrapperList = (IList)wrapper; int len = wrapperList.Count; IList list; Type elem_type; if (fi.FieldType.IsArray) { elem_type = fi.FieldType.GetElementType(); list = (IList)Activator.CreateInstance(fi.FieldType, len); for (int i = 0; i < len; i++) { list[i] = wrapperList[i] == null ? null : ((IJsonWrapper)wrapperList[i]).ToObject(elem_type); } } else { elem_type = fi.FieldType.GetGenericArguments()[0]; list = (IList)Activator.CreateInstance(fi.FieldType); for (int i = 0; i < len; i++) { list.Add(wrapperList[i] == null ? null : ((IJsonWrapper)wrapperList[i]).ToObject(elem_type)); } } fi.SetValue(instance, list); } else { object value = JsonMapper.ReadBaseTypeProperty(fi.FieldType, ToBasePropertyValue(wrapper)); fi.SetValue(instance, value); } } } foreach (PropertyInfo pi in t.GetProperties()) { var name = pi.Name;//获得属性的名字,后面就可以根据名字判断来进行些自己想要的操作 if (pi.CanWrite && ((IDictionary)this).Contains(name)) { IJsonWrapper wrapper = this[name]; if (wrapper == null) { pi.SetValue(instance, null, null); } else if (wrapper.IsObject) { pi.SetValue(instance, wrapper.ToObject(pi.PropertyType), null); } else if (wrapper.IsArray) { IList wrapperList = (IList)wrapper; int len = wrapperList.Count; IList list; Type elem_type; if (pi.PropertyType.IsArray) { elem_type = pi.PropertyType.GetElementType(); list = (IList)Activator.CreateInstance(pi.PropertyType, len); for (int i = 0; i < len; i++) { list[i] = wrapperList[i] == null ? null : ((IJsonWrapper)wrapperList[i]).ToObject(elem_type); } } else { elem_type = pi.PropertyType.GetGenericArguments()[0]; list = (IList)Activator.CreateInstance(pi.PropertyType, len); for (int i = 0; i < len; i++) { list.Add(wrapperList[i] == null ? null : ((IJsonWrapper)wrapperList[i]).ToObject(elem_type)); } } pi.SetValue(instance, list, null); } else { object value = JsonMapper.ReadBaseTypeProperty(pi.PropertyType, ToBasePropertyValue(wrapper)); pi.SetValue(instance, value, null); } } } return(instance); }