/// <summary>
        /// Creates a new instance of type.
        /// First looks at JsConfig.ModelFactory before falling back to CreateInstance
        /// </summary>
        public static object New(this Type type)
        {
            var factoryFn = JsConfig.ModelFactory(type)
                            ?? GetConstructorMethod(type);

            return(factoryFn());
        }
        /// <summary>
        /// Creates a new instance of type.
        /// First looks at JsConfig.ModelFactory before falling back to CreateInstance
        /// </summary>
        public static T New <T>(this Type type)
        {
            var factoryFn = JsConfig.ModelFactory(type)
                            ?? GetConstructorMethod(type);

            return((T)factoryFn());
        }
        /// <summary>
        /// Creates a new instance of type.
        /// First looks at JsConfig.ModelFactory before falling back to CreateInstance
        /// </summary>
        public static object New(this Type type)
        {
            //var factoryFn = JsConfig.ModelFactory(type) ?? GetConstructorMethod(type);
            //return factoryFn();
            var factoryFn = JsConfig.ModelFactory(type);

            return(factoryFn != null?factoryFn() : ActivatorUtils.FastCreateInstance(type));
        }
Пример #4
0
        internal static ParseStringSegmentDelegate GetParseStringSegmentMethod(TypeConfig typeConfig)
        {
            var type = typeConfig.Type;

            if (!type.IsStandardClass())
            {
                return(null);
            }
            var map = DeserializeTypeRef.GetTypeAccessorMap(typeConfig, Serializer);

            var ctorFn = JsConfig.ModelFactory(type);

            if (map == null)
            {
                return(value => ctorFn());
            }

            return(typeof(TSerializer) == typeof(Json.JsonTypeSerializer)
                ? (ParseStringSegmentDelegate)(value => DeserializeTypeRefJson.StringToType(typeConfig, value, ctorFn, map))
                : value => DeserializeTypeRefJsv.StringToType(typeConfig, value, ctorFn, map));
        }
        public static ParseStringDelegate GetParseMethod(TypeConfig typeConfig)
        {
            var type = typeConfig.Type;

            if (!type.IsClass || type.IsAbstract || type.IsInterface)
            {
                return(null);
            }

            var map = DeserializeTypeRef.GetTypeAccessorMap(typeConfig, Serializer);

            var ctorFn = JsConfig.ModelFactory(type);

            if (map == null)
            {
                return(value => ctorFn());
            }

            return(typeof(TSerializer) == typeof(Json.JsonTypeSerializer)
                                ? (ParseStringDelegate)(value => DeserializeTypeRefJson.StringToType(type, value, ctorFn, map))
                                : value => DeserializeTypeRefJsv.StringToType(type, value, ctorFn, map));
        }
        internal static ParseStringSpanDelegate GetParseStringSpanMethod(TypeConfig typeConfig)
        {
            var type = typeConfig.Type;

            if (!type.IsStandardClass())
            {
                return(null);
            }
            var accessors = DeserializeTypeRef.GetTypeAccessors(typeConfig, Serializer);

            var ctorFn = JsConfig.ModelFactory(type);

            if (accessors == null)
            {
                return(value => ctorFn());
            }

            if (typeof(TSerializer) == typeof(Json.JsonTypeSerializer))
            {
                return(new StringToTypeContext(typeConfig, ctorFn, accessors).DeserializeJson);
            }

            return(new StringToTypeContext(typeConfig, ctorFn, accessors).DeserializeJsv);
        }