예제 #1
0
        /// <summary>
        /// Create an instance of <see cref="IXSerializer"/> for the given type using the specified configuration.
        /// </summary>
        /// <param name="type">The type of the object that the serializer will operate on.</param>
        /// <param name="configuration">The configuration for the serializer.</param>
        /// <returns>An instance of <see cref="IXSerializer"/>.</returns>
        /// <remarks>
        /// An instance of the generic <see cref="JsonSerializer{T}"/> class of type <paramref name="type"/>
        /// is returned from this method.
        /// </remarks>
        public static IXSerializer Create(Type type, IJsonSerializerConfiguration configuration)
        {
            var createJsonSerializer = _createXmlSerializerFuncs.GetOrAdd(
                type, t =>
                {
                    var jsonSerializerType = typeof(JsonSerializer<>).MakeGenericType(t);
                    var ctor = jsonSerializerType.GetConstructor(new[] { typeof(IJsonSerializerConfiguration) });

                    if (ctor == null) throw new InvalidOperationException("A source code change has resulted in broken reflection. typeof(JsonSerializer<>).MakeGenericType(type).GetConstructor(new[] { typeof(IJsonSerializerConfiguration) })");

                    var parameter = Expression.Parameter(typeof(IJsonSerializerConfiguration), "configuration");

                    var lambda = Expression.Lambda<Func<IJsonSerializerConfiguration, IXSerializer>>(
                        Expression.New(ctor, parameter),
                        parameter);

                    return lambda.Compile();
                });

            return createJsonSerializer(configuration ?? new JsonSerializerConfiguration());
        }
예제 #2
0
        /// <summary>
        /// Create an instance of <see cref="IXSerializer"/> for the given type using the specified configuration.
        /// </summary>
        /// <param name="type">The type of the object that the serializer will operate on.</param>
        /// <param name="configuration">The configuration for the serializer.</param>
        /// <returns>An instance of <see cref="IXSerializer"/>.</returns>
        /// <remarks>
        /// An instance of the generic <see cref="JsonSerializer{T}"/> class of type <paramref name="type"/>
        /// is returned from this method.
        /// </remarks>
        public static IXSerializer Create(Type type, IJsonSerializerConfiguration configuration)
        {
            var createJsonSerializer = _createXmlSerializerFuncs.GetOrAdd(
                type, t =>
            {
                var jsonSerializerType = typeof(JsonSerializer <>).MakeGenericType(t);
                var ctor = jsonSerializerType.GetConstructor(new[] { typeof(IJsonSerializerConfiguration) });

                if (ctor == null)
                {
                    throw new InvalidOperationException("A source code change has resulted in broken reflection. typeof(JsonSerializer<>).MakeGenericType(type).GetConstructor(new[] { typeof(IJsonSerializerConfiguration) })");
                }

                var parameter = Expression.Parameter(typeof(IJsonSerializerConfiguration), "configuration");

                var lambda = Expression.Lambda <Func <IJsonSerializerConfiguration, IXSerializer> >(
                    Expression.New(ctor, parameter),
                    parameter);

                return(lambda.Compile());
            });

            return(createJsonSerializer(configuration ?? new JsonSerializerConfiguration()));
        }