public static string SerializeType(Type type, ButlerSerializerSettings settings) { object instance = ButlerActivator.CreateInstance(type, settings); string instanceSerialized = JsonConvert.SerializeObject(instance, settings.JsonSerializerSettings); return(instanceSerialized); }
public static object CreateInstance(Type type, ButlerSerializerSettings settings) { // Bail/simplify external objects first. if (type.Assembly != settings.RootCallingAssembly) { return(CreateDefaultInstance(type)); } ConstructorInfo constructorInfo = ReflectionUtilities.GetPreferredConstructor(type, settings.PreferredAttributeTypesOnConstructor); if (type.IsValueType && constructorInfo == null) { return(Activator.CreateInstance(type)); } return(CreateInstanceWithConstructor(type, settings, constructorInfo)); }
private static object CreateInstanceWithConstructor(Type type, ButlerSerializerSettings settings, ConstructorInfo constructorInfo) { ParameterInfo[] parameterInfos = constructorInfo.GetParameters(); if (parameterInfos.Length == 0) { return(Activator.CreateInstance(type)); } object[] parameterObjects = new object[parameterInfos.Length]; for (int i = 0; i < parameterInfos.Length; i++) { Type parameterType = parameterInfos[i].ParameterType; parameterObjects[i] = CreateInstance(parameterType, settings); } object instance = constructorInfo.Invoke(parameterObjects); return(instance); }
public static object CreateInstance <T> (ButlerSerializerSettings settings) { return(CreateInstance(typeof(T), settings)); }
public static string SerializeType <T> (ButlerSerializerSettings settings) { return(SerializeType(typeof(T), settings)); }