Пример #1
0
        /// <summary>
        /// Create an instance of <see cref="IXSerializer"/> for the given type using a default configuration.
        /// </summary>
        /// <param name="type">The type of the object that the serializer will operate on.</param>
        /// <param name="options">Options.</param>
        /// <param name="extraTypes">Extra types that can be serialized.</param>
        /// <returns>An instance of <see cref="IXSerializer"/>.</returns>
        /// <remarks>
        /// An instance of the generic <see cref="XmlSerializer{T}"/> class of type <paramref name="type"/>
        /// is returned from this method.
        /// </remarks>
        public static IXSerializer Create(Type type, XmlSerializationOptions options, params Type[] extraTypes)
        {
            var createXmlSerializer = _createXmlSerializerFuncs.GetOrAdd(
                type,
                t =>
            {
                var xmlSerializerType = typeof(XmlSerializer <>).MakeGenericType(t);
                var ctor = xmlSerializerType.GetConstructor(new[] { typeof(XmlSerializationOptions), typeof(Type[]) });

                if (ctor == null)
                {
                    throw new InvalidOperationException("A source code change has resulted in broken reflection. typeof(XmlSerializer<>).MakeGenericType(type).GetConstructor(new[] { typeof(XmlSerializationOptions), typeof(Type[]) })");
                }
                var optionsParameter    = Expression.Parameter(typeof(XmlSerializationOptions), "options");
                var extraTypesParameter = Expression.Parameter(typeof(Type[]), "extraTypes");

                var lambda =
                    Expression.Lambda <Func <XmlSerializationOptions, Type[], IXSerializer> >(
                        Expression.New(ctor, optionsParameter, extraTypesParameter),
                        optionsParameter,
                        extraTypesParameter);

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

            return(createXmlSerializer(options, extraTypes));
        }
Пример #2
0
        internal static XmlSerializationOptions GetSerializationOptions(Action <XmlSerializationOptions> setOptions)
        {
            var options = new XmlSerializationOptions();

            if (setOptions != null)
            {
                setOptions(options);
            }

            return(options);
        }
Пример #3
0
        /// <summary>
        /// Create an instance of <see cref="IXSerializer"/> for the given type using a default configuration.
        /// </summary>
        /// <param name="type">The type of the object that the serializer will operate on.</param>
        /// <param name="options">Options.</param>
        /// <param name="extraTypes">Extra types that can be serialized.</param>
        /// <returns>An instance of <see cref="IXSerializer"/>.</returns>
        /// <remarks>
        /// An instance of the generic <see cref="XmlSerializer{T}"/> class of type <paramref name="type"/>
        /// is returned from this method.
        /// </remarks>
        public static IXSerializer Create(Type type, XmlSerializationOptions options, params Type[] extraTypes)
        {
            var createXmlSerializer = _createXmlSerializerFuncs.GetOrAdd(
                type,
                t =>
                {
                    var xmlSerializerType = typeof(XmlSerializer<>).MakeGenericType(t);
                    var ctor = xmlSerializerType.GetConstructor(new[] { typeof(XmlSerializationOptions), typeof(Type[]) });

                    if (ctor == null) throw new InvalidOperationException("A source code change has resulted in broken reflection. typeof(XmlSerializer<>).MakeGenericType(type).GetConstructor(new[] { typeof(XmlSerializationOptions), typeof(Type[]) })");
                    var optionsParameter = Expression.Parameter(typeof(XmlSerializationOptions), "options");
                    var extraTypesParameter = Expression.Parameter(typeof(Type[]), "extraTypes");

                    var lambda =
                        Expression.Lambda<Func<XmlSerializationOptions, Type[], IXSerializer>>(
                            Expression.New(ctor, optionsParameter, extraTypesParameter),
                            optionsParameter,
                            extraTypesParameter);

                    return lambda.Compile();
                });

            return createXmlSerializer(options, extraTypes);
        }
Пример #4
0
        internal static XmlSerializationOptions GetSerializationOptions(Action<XmlSerializationOptions> setOptions)
        {
            var options = new XmlSerializationOptions();

            if (setOptions != null)
            {
                setOptions(options);
            }

            return options;
        }