public void RegisterAllUniqueInterfaceImplementations(bool overwriteExistingRegistrations = false, params Assembly[] assemblies) { if (assemblies == null || !assemblies.Any()) { assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(x => !x.IsDynamic).ToArray(); } var classTypes = assemblies.SelectMany(x => x.GetTypes()).Where(x => !x.IsInterface && !x.IsAbstract).ToList(); var interfaces = new HashSet <Type>(assemblies.SelectMany(x => x.GetTypes()).Where(x => x.IsInterface)); var interfaceMap = new HashSetDictionary <Type, Type>(); foreach (var classType in classTypes) { foreach (var interfaceType in classType.GetInterfaces().Where(x => interfaces.Contains(x))) { interfaceMap.Add(interfaceType, classType); } } foreach (var interfaceType in interfaceMap.Keys) { var values = interfaceMap.GetValuesAsHashSet(interfaceType); if (values.Count != 1) { continue; } if (overwriteExistingRegistrations || !_actions.ContainsKey(interfaceType)) { Register(interfaceType, values.First()); } } }
public void TestSerialization() { var lookup = new HashSetDictionary <string, string>(); lookup.Add("zoo", "zebra"); var tester = new SerializationTester(); tester.TestSerialization(lookup, result => { Assert.IsTrue(result["zoo"].Contains("zebra")); }); }
public void If_adding_single_item_Then_expected_count_and_values_match() { _sut.Add(1, 1000); Assert.That(_sut.Count, Is.EqualTo(1)); Assert.That(_sut.ContainsKey(1), Is.True); Assert.That(_sut.GetValues(1), Is.EquivalentTo(new[] { 1000 })); Assert.That(_sut.GetValuesAsHashSet(1), Is.EquivalentTo(new[] { 1000 })); Assert.That(_sut.Keys, Is.EquivalentTo(new[] { 1 })); Assert.That(_sut.Values, Is.EquivalentTo(new[] { 1000 })); }
public void Then_adding_single_int_keys_are_benchmarked() { var sut = new HashSetDictionary <int, int>(); var sw = Stopwatch.StartNew(); for (var i = 0; i < Count; i++) { sut.Add(1, i); } sw.Stop(); var opsPerSec = Count / (sw.ElapsedMilliseconds + 0.001m) * 1000m; Assert.Inconclusive($"{Count} iterations of Add multiple keys and value, {sw.Elapsed} ({opsPerSec:N0} ops/sec)"); }
public MimeList(string body) { string[] lines = body._GetLines(); HashSetDictionary <string, string> extToMime = new HashSetDictionary <string, string>(StrComparer.IgnoreCaseComparer, StrComparer.IgnoreCaseComparer); HashSetDictionary <string, string> mimeToExt = new HashSetDictionary <string, string>(StrComparer.IgnoreCaseComparer, StrComparer.IgnoreCaseComparer); foreach (string line in lines) { string line2 = line._StripCommentFromLine(Consts.Strings.CommentStartStringForMimeList)._NonNullTrim(); if (line2._IsFilled()) { if (line2._GetKeyAndValue(out string key, out string value)) { key = key.ToLowerInvariant(); value = value.ToLowerInvariant(); if (key.StartsWith(".")) { key = key.Substring(1); } if (key._IsFilled() && value._IsFilled()) { extToMime.Add(key, value); mimeToExt.Add(value, key); if (line._InStr("#overwrite") || line._InStr("# overwrite")) { this.ExtToMimeDictionary.TryAdd(key, value); } } } } } foreach (var extInfo in extToMime) { string?mime = extInfo.Value.OrderBy(x => mimeToExt[x].Count).FirstOrDefault(); if (mime._IsFilled()) { this.ExtToMimeDictionary.TryAdd(extInfo.Key, mime); } } }
static void _Main(string[] args) // De-activated { var lookup = new HashSetDictionary <string, string>(); lookup.Add("zoo", "zebra"); using (var stream = new MemoryStream()) { var formatter = new BinaryFormatter(); formatter.Serialize(stream, lookup); stream.Seek(0, SeekOrigin.Begin); var result = (HashSetDictionary <string, string>)formatter.Deserialize(stream); Console.WriteLine(lookup["zoo"].Contains("zebra")); } Console.ReadKey(); }