private string SimpleTypeToJsonString(Type type) { object instance = null; try { instance = Activator.CreateInstance(type); } catch { } if (instance == null) { return("cannot create instance of this type!"); } return(ServerSerializationHelper.SerializeObject(instance, null, NullValueHandling.Include)); }
private string TypeToJsonString(Type type) { List <Type> createdInstance = new List <Type>(); return(ServerSerializationHelper.SerializeObject(CreateInstances(type, createdInstance), null, NullValueHandling.Include)); object CreateInstances(Type newType, List <Type> items) { if (items.Contains(newType)) { return(DataExchangeConverter.GetDefault(newType)); } items.Add(newType); object result = null; SerializeObjectType typeCode = SerializeHelper.GetTypeCodeOfObject(newType); if (typeCode == SerializeObjectType.Object) { #if (NETSTANDARD1_6 || NETCOREAPP1_1) if (!newType.GetTypeInfo().IsInterface) #else if (!newType.IsInterface) #endif { try { result = Activator.CreateInstance(newType); foreach (PropertyInfo item in newType.GetProperties()) { item.SetValue(result, CreateInstances(item.PropertyType, items), null); } } catch (Exception) { } } else if (newType.GetGenericTypeDefinition() == typeof(ICollection <>) || newType.GetGenericTypeDefinition() == typeof(IList <>)) { try { Type gType = newType.GetListOfGenericArguments().FirstOrDefault(); Type listType = typeof(List <>).MakeGenericType(gType); result = Activator.CreateInstance(listType); } catch { } } } else { result = DataExchangeConverter.GetDefault(newType); } #if (NETSTANDARD1_6 || NETCOREAPP1_1) if (newType.GetTypeInfo().IsGenericType&& result != null) #else if (newType.IsGenericType && result != null) #endif { if (newType.GetGenericTypeDefinition() == typeof(List <>) || newType.GetGenericTypeDefinition() == typeof(ICollection <>) || newType.GetGenericTypeDefinition() == typeof(IList <>)) { Type gType = newType.GetListOfGenericArguments().FirstOrDefault(); if (gType != null) { try { object gResult = Activator.CreateInstance(gType); foreach (PropertyInfo item in gType.GetProperties()) { item.SetValue(gResult, CreateInstances(item.PropertyType, items), null); } result.GetType().GetMethod("Add").Invoke(result, new object[] { gResult }); //col.Add(gResult); } catch { } } } } return(result); } }