#region License /* FNA - XNA4 Reimplementation for Desktop Platforms * Copyright 2009-2016 Ethan Lee and the MonoGame Team * * Released under the Microsoft Public License. * See LICENSE for details. */ #endregion #region Using Statements using System; using System.Collections.Generic; #endregion namespace Microsoft.Xna.Framework.Content { internal class DictionaryReader : ContentTypeReader> { #region Public Properties public override bool CanDeserializeIntoExistingObject { get { return true; } } #endregion #region Private Variables ContentTypeReader keyReader; ContentTypeReader valueReader; Type keyType; Type valueType; #endregion #region Public Constructor public DictionaryReader() { } #endregion #region Protected Initialization Method protected internal override void Initialize(ContentTypeReaderManager manager) { keyType = typeof(TKey); valueType = typeof(TValue); keyReader = manager.GetTypeReader(keyType); valueReader = manager.GetTypeReader(valueType); } #endregion #region Protected Read Method protected internal override Dictionary Read(ContentReader input, Dictionary existingInstance) { int count = input.ReadInt32(); Dictionary dictionary = existingInstance; if (dictionary == null) { dictionary = new Dictionary(count); } else { dictionary.Clear(); } for (int i = 0; i < count; i += 1) { TKey key; TValue value; if (keyType.IsValueType) { key = input.ReadObject(keyReader); } else { int readerType = input.Read7BitEncodedInt(); key = (readerType > 0) ? input.ReadObject(input.TypeReaders[readerType - 1]) : default(TKey); } if (valueType.IsValueType) { value = input.ReadObject(valueReader); } else { int readerType = input.Read7BitEncodedInt(); value = (readerType > 0) ? input.ReadObject(input.TypeReaders[readerType - 1]) : default(TValue); } dictionary.Add(key, value); } return dictionary; } #endregion } }