private void SerializeDictionary(IDictionaryWrapper dictionary, int indentLevel) { var appendSeparator = false; if (dictionary != null) { _serialized.Append(Constants.OPEN_CURLY); AppendType(dictionary.Dictionary, SerializationTypeHandle.Collections, ++indentLevel, Constants.COMMA.ToString()); foreach (var key in dictionary.Keys) { AppendSeparator(appendSeparator, indentLevel); SerializeObject(key.GetType(), key, indentLevel); _serialized.Append(Constants.COLON); PrettyPrintSpace(); SerializeObject(dictionary[key].GetType(), dictionary[key], indentLevel); appendSeparator = true; } PrettyPrintNewLine(); PrettyPrintIndent(--indentLevel); _serialized.Append(Constants.CLOSE_CURLY); return; } _serialized.Append(Constants.NULL); }
protected object GetDictionaryValues(IDictionaryWrapper dictionaryWrapper) { var keyType = dictionaryWrapper.KeyType; var valueType = dictionaryWrapper.ValueType; foreach (var child in Children) { if (child.Name == "$type") { continue; } var key = child.Name; var token = new Tokenizer().Tokenize(key, SettingsManager); token.OnNullValue = ReturnNull; var keyValue = token.GetValue(keyType); if (keyValue == null) { if (SettingsManager.SkipNullKeysInKeyValuedCollections) { continue; } throw new NullReferenceException("Cannot add null key to dictionary."); } dictionaryWrapper[keyValue] = child.GetValue(valueType); } return(dictionaryWrapper.Dictionary); }
internal static IDictionaryWrapper CreateDictionaryWrapper(object dictionary, Type dictType, Type keyType, Type valueType) { var wrapperType = typeof(DictionaryWrapper <,>).MakeGenericType(keyType, valueType); ConstructorInfo genericWrapperConstructor = wrapperType.GetConstructor(new[] { dictType }); IDictionaryWrapper wrapper = (IDictionaryWrapper)genericWrapperConstructor?.Invoke(new [] { dictionary }); return(wrapper); }
public void run() { Console.WriteLine("TestDictionaryWithIndexer1"); IDictionaryWrapper <string, int> wrapper = new IDictionaryWrapper <string, int>(); wrapper["One"] = 1; //adding a key/value using the Add() method wrapper["Two"] = 2; wrapper["Three"] = 3; Console.WriteLine("One: {0}, Two: {1}, Three: {2}", wrapper["One"], wrapper["Two"], wrapper["Three"]); }
private static long Benchmark(IDictionaryWrapper target, int numberOfItems = 1000) { var list = new List<string>(); for (int i = 0; i < numberOfItems; ++i) { list.Add(i.ToString()); } target.performWrite(list); var watch = new Stopwatch(); watch.Start(); foreach(var value in target.ReadNext()) { cntr += value.Length; } watch.Stop(); return watch.Elapsed.Ticks; }
protected bool IsGenericDictionary(object obj, Type type, out IDictionaryWrapper dictionaryWrapper) { dictionaryWrapper = null; foreach (var interfaceType in obj.GetType().GetInterfaces()) { if (interfaceType.IsGenericType && (interfaceType.GetGenericTypeDefinition() == typeof(IDictionary <,>))) { var genArgs = interfaceType.GenericTypeArguments; dictionaryWrapper = Serializer.CreateDictionaryWrapper(obj, type, genArgs[0], genArgs[1]); return(true); } } if (typeof(IDictionary).IsAssignableFrom(type)) { dictionaryWrapper = Serializer.CreateDictionaryWrapper(obj); return(true); } return(false); }
public void EnumeratorTest() { ListDictionary dict = new ListDictionary(); dict.Add("a", 5); IDictionaryWrapper<string, int> genDict = new IDictionaryWrapper<string, int>(dict); int count = 0; foreach (System.Collections.Generic.KeyValuePair<string, int> pair in genDict) { Assert.AreEqual("a", pair.Key); Assert.AreEqual(5, pair.Value); Assert.AreEqual(1, ++count); } }