Ejemplo n.º 1
0
        /// <summary>
        /// Adds a new converter that can be used to customize how an object is serialized and
        /// deserialized.
        /// </summary>
        public void AddConverter(fsBaseConverter converter)
        {
            if (converter.Serializer != null)
            {
                throw new InvalidOperationException("Cannot add a single converter instance to " +
                                                    "multiple fsConverters -- please construct a new instance for " + converter);
            }

            // TODO: wrap inside of a ConverterManager so we can control _converters and _cachedConverters lifetime
            if (converter is fsDirectConverter)
            {
                var directConverter = (fsDirectConverter)converter;
                _availableDirectConverters[directConverter.ModelType] = directConverter;
            }
            else if (converter is fsConverter)
            {
                _availableConverters.Insert(0, (fsConverter)converter);
            }
            else
            {
                throw new InvalidOperationException("Unable to add converter " + converter +
                                                    "; the type association strategy is unknown. Please use either " +
                                                    "fsDirectConverter or fsConverter as your base type.");
            }

            converter.Serializer = this;

            // We need to reset our cached converter set, as it could be invalid with the new
            // converter. Ideally, _cachedConverters should be empty (as the user should fully setup
            // the serializer before actually using it), but there is no guarantee.
            _cachedConverters = new Dictionary <Type, fsBaseConverter>();
        }
Ejemplo n.º 2
0
        /// Adds a new converter that can be used to customize how an object is serialized and
        /// deserialized.
        public void AddConverter(fsBaseConverter converter)
        {
            if (converter.Serializer != null)
            {
                throw new InvalidOperationException("Cannot add a single converter instance to " +
                                                    "multiple fsConverters -- please construct a new instance for " + converter);
            }

            if (converter is fsDirectConverter)
            {
                var directConverter = (fsDirectConverter)converter;
                _availableDirectConverters[directConverter.ModelType] = directConverter;
            }
            else if (converter is fsConverter)
            {
                _availableConverters.Insert(0, (fsConverter)converter);
            }
            else
            {
                throw new InvalidOperationException("Unable to add converter " + converter +
                                                    "; the type association strategy is unknown. Please use either " +
                                                    "fsDirectConverter or fsConverter as your base type.");
            }

            converter.Serializer = this;
            _cachedConverters    = new Dictionary <Type, fsBaseConverter>();
        }