public void CreateObjectDictionary <KeyType, ValueType>(Dictionary <KeyType, ValueType> dictionaryToPopulate, string contentManagerName) { Type typeOfElement = typeof(ValueType); #if DEBUG #if WINDOWS_8 || UWP bool isPrimitive = typeOfElement.IsPrimitive(); #else bool isPrimitive = typeOfElement.IsPrimitive; #endif if (isPrimitive || typeOfElement == typeof(string)) { throw new InvalidOperationException("Can't create dictionaries of primitives or strings because they don't have a key"); } #endif MemberTypeIndexPair[] memberTypeIndexPairs; IEnumerable <PropertyInfo> propertyInfosEnumerable; IEnumerable <FieldInfo> fieldInfosEnumerable; GetReflectionInformation(typeOfElement, out memberTypeIndexPairs, out propertyInfosEnumerable, out fieldInfosEnumerable); List <PropertyInfo> propertyInfos = new List <PropertyInfo>(propertyInfosEnumerable); List <FieldInfo> fieldInfos = new List <FieldInfo>(fieldInfosEnumerable); #region Get the required header which we'll use for the key CsvHeader csvHeaderForKey = CsvHeader.Empty; int headerIndex = 0; foreach (CsvHeader header in Headers) { if (header.IsRequired) { csvHeaderForKey = header; break; } headerIndex++; } if (csvHeaderForKey == CsvHeader.Empty) { throw new InvalidOperationException("Could not find a property to use as the key. One of the columns needs to be marked as required. " + "For example \"Name (string, required)\""); } #endregion int numberOfColumns = Headers.Length; object lastElement = null; bool wasRequiredMissing = false; for (int row = 0; row < Records.Count; row++) { object newElement; bool newElementFailed; wasRequiredMissing = TryCreateNewObjectFromRow( typeOfElement, contentManagerName, memberTypeIndexPairs, propertyInfos, fieldInfos, numberOfColumns, lastElement, wasRequiredMissing, row, out newElement, out newElementFailed); if (!newElementFailed && !wasRequiredMissing) { KeyType keyToUse = GetKeyToUse <KeyType, ValueType>(typeOfElement, csvHeaderForKey, headerIndex, newElement); if (dictionaryToPopulate.ContainsKey(keyToUse)) { throw new InvalidOperationException("The key " + keyToUse + " is already part of the dictionary."); } else { dictionaryToPopulate.Add(keyToUse, (ValueType)newElement); } lastElement = newElement; } } }