public static JObject Serialize(Type type, object oSource) { var result = new JObject(); var methods = type.GetMethods(); foreach (var m in methods) { if (!m.IsPublic) { continue; } if (m.Name.IndexOf("get_") == 0) { var name = m.Name.Substring(4); var methodResult = m.Invoke(oSource, null); if (methodResult == null) { result._members.Add(name.ToLower(), new JProperty(name, JValue.Serialize(m.ReturnType, null))); } if (m.ReturnType.IsArray) { result._members.Add(name.ToLower(), new JProperty(name, JArray.Serialize(m.ReturnType, methodResult))); } else { result._members.Add(name.ToLower(), new JProperty(name, JObject.Serialize(m.ReturnType, methodResult))); } } } var fields = type.GetFields(); foreach (var f in fields) { if (f.FieldType.IsNotPublic) { continue; } switch (f.MemberType) { case MemberTypes.Field: case MemberTypes.Property: var value = f.GetValue(oSource); if (value == null) { result._members.Add(f.Name, new JProperty(f.Name, JValue.Serialize(f.FieldType, null))); } else if (f.FieldType.IsValueType || f.FieldType == typeof(string)) { result._members.Add(f.Name.ToLower(), new JProperty(f.Name, JValue.Serialize(f.FieldType, value))); } else { if (f.FieldType.IsArray) { result._members.Add(f.Name.ToLower(), new JProperty(f.Name, JArray.Serialize(f.FieldType, value))); } else { result._members.Add(f.Name.ToLower(), new JProperty(f.Name, JObject.Serialize(f.FieldType, value))); } } break; default: break; } } return(result); }