internal object InitializeCollection(BinaryReader br, bool addToCache, DataTypeDescriptor descriptor, DeserializationManager manager, out int count) { object result; // KeyValuePair, DictionaryEntry if (IsSingleElement) { // Note: If addToCache is true, then the key-value may contain itself via references. // That's why we create the instance first and then set Key and Value (just like at object graphs). result = Activator.CreateInstance(descriptor.GetTypeToCreate()); if (addToCache) { manager.AddObjectToCache(result); } count = 1; return(result); } // 1.) Count count = Read7BitInt(br); // 2.) Capacity int capacity = HasCapacity ? Read7BitInt(br) : count; // 3.) Case sensitivity bool caseInsensitive = false; if (HasCaseInsensitivity) { caseInsensitive = br.ReadBoolean(); } // 4.) Read-only if (HasReadOnly) { descriptor.IsReadOnly = br.ReadBoolean(); } // In the ID cache the collection comes first and then the comparer so we add a placeholder to the cache. // Unlike for KeyValuePairs this works here because we can assume that a comparer does not reference the collection. int id = 0; if (addToCache) { manager.AddObjectToCache(null, out id); } // 5.) Comparer object comparer = HasAnyComparer ? br.ReadBoolean() ? IsNonNullDefaultComparer ? GetDefaultComparer(descriptor.Type) : null : manager.ReadWithType(br) : null; result = CreateCollection(descriptor, capacity, caseInsensitive, comparer); if (id != 0) { manager.ReplaceObjectInCache(id, result); } return(result); }