예제 #1
0
        public static string SerializeType(Type type, ButlerSerializerSettings settings)
        {
            object instance           = ButlerActivator.CreateInstance(type, settings);
            string instanceSerialized = JsonConvert.SerializeObject(instance, settings.JsonSerializerSettings);

            return(instanceSerialized);
        }
예제 #2
0
        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));
        }
예제 #3
0
        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);
        }
예제 #4
0
 public static object CreateInstance <T> (ButlerSerializerSettings settings)
 {
     return(CreateInstance(typeof(T), settings));
 }
예제 #5
0
 public static string SerializeType <T> (ButlerSerializerSettings settings)
 {
     return(SerializeType(typeof(T), settings));
 }