示例#1
0
        public void Nums_Abs()
        {
            var nums  = new[] { 47, -32, -54, 18, 62, -71, 58 };
            var byAbs = new SortedSet <int>(nums, KeyComparer.Create((int n) => Math.Abs(n)));

            EnumerableAssert.AreEqual(new[] { 18, -32, 47, -54, 58, 62, -71 }, byAbs);
        }
示例#2
0
        public void Compare_Nulls()
        {
            var comparer = KeyComparer.Create((City city) => city.Name);

            CompareAssert.IsLessThan(null, athens, comparer);
            CompareAssert.IsEqualTo(null, null, comparer);
            CompareAssert.IsGreaterThan(athens, null, comparer);
        }
示例#3
0
        public DecompilerTypeSystem(ModuleDefinition moduleDefinition)
        {
            if (moduleDefinition == null)
            {
                throw new ArgumentNullException(nameof(moduleDefinition));
            }
            this.moduleDefinition = moduleDefinition;
            CecilLoader cecilLoader = new CecilLoader {
                IncludeInternalMembers = true, LazyLoad = true, OnEntityLoaded = StoreMemberReference, ShortenInterfaceImplNames = false
            };

            typeReferenceCecilLoader.SetCurrentModule(moduleDefinition);
            IUnresolvedAssembly mainAssembly = cecilLoader.LoadModule(moduleDefinition);
            // Load referenced assemblies and type-forwarder references.
            // This is necessary to make .NET Core/PCL binaries work better.
            var referencedAssemblies        = new List <IUnresolvedAssembly>();
            var assemblyReferenceQueue      = new Queue <AssemblyNameReference>(moduleDefinition.AssemblyReferences);
            var processedAssemblyReferences = new HashSet <AssemblyNameReference>(KeyComparer.Create((AssemblyNameReference reference) => reference.FullName));

            while (assemblyReferenceQueue.Count > 0)
            {
                var asmRef = assemblyReferenceQueue.Dequeue();
                if (!processedAssemblyReferences.Add(asmRef))
                {
                    continue;
                }
                var asm = moduleDefinition.AssemblyResolver.Resolve(asmRef);
                if (asm != null)
                {
                    referencedAssemblies.Add(cecilLoader.LoadAssembly(asm));
                    foreach (var forwarder in asm.MainModule.ExportedTypes)
                    {
                        if (!forwarder.IsForwarder || !(forwarder.Scope is AssemblyNameReference forwarderRef))
                        {
                            continue;
                        }
                        assemblyReferenceQueue.Enqueue(forwarderRef);
                    }
                }
            }
            compilation = new SimpleCompilation(mainAssembly, referencedAssemblies);
            // Primitive types are necessary to avoid assertions in ILReader.
            // Fallback to MinimalCorlib to provide the primitive types.
            if (compilation.FindType(KnownTypeCode.Void).Kind == TypeKind.Unknown || compilation.FindType(KnownTypeCode.Int32).Kind == TypeKind.Unknown)
            {
                referencedAssemblies.Add(MinimalCorlib.Instance);
                compilation = new SimpleCompilation(mainAssembly, referencedAssemblies);
            }
            context = new SimpleTypeResolveContext(compilation.MainAssembly);
        }
示例#4
0
        public void Compare()
        {
            using (new CultureSwapper(PredefinedCulture.EnglishUnitedStates))
            {
                var message  = "'ñ' should be less than 't' for default comparison (current culture, en-US).";
                var comparer = KeyComparer.Create((City city) => city.Name);
                CompareAssert.IsLessThan(añasco, athens, comparer, message);
                CompareAssert.IsEqualTo(añasco, añasco, comparer);
                CompareAssert.IsGreaterThan(athens, añasco, comparer, message);

                message  = "'ñ' should greater less than 't' for ordinal comparison.";
                comparer = KeyComparer.Create((City city) => city.Name, StringComparer.Ordinal);
                CompareAssert.IsGreaterThan(añasco, athens, comparer, message);
                CompareAssert.IsEqualTo(añasco, añasco, comparer);
                CompareAssert.IsLessThan(athens, añasco, comparer, message);
            }
        }
示例#5
0
        public void Sorting()
        {
            using (new CultureSwapper(PredefinedCulture.EnglishUnitedStates))
            {
                var cities = new[] { paris, newYork, athens, madrid, añasco };

                var sortedSet = new SortedSet <City>(cities, KeyComparer.Create((City city) => city.Name));
                EnumerableAssert.AreEqual(new[] { añasco, athens, madrid, newYork, paris }, sortedSet);

                sortedSet = new SortedSet <City>(cities, KeyComparer.Create((City city) => city.Name, StringComparer.Ordinal));
                EnumerableAssert.AreEqual(new[] { athens, añasco, madrid, newYork, paris }, sortedSet);
            }

            int[] nums  = new[] { 47, -32, -54, 18, 62, -71, 58 };
            var   byAbs = new SortedSet <int>(nums, KeyComparer.Create((int n) => Math.Abs(n)));

            EnumerableAssert.AreEqual(new [] { 18, -32, 47, -54, 58, 62, -71 }, byAbs);
        }
示例#6
0
        public void Person_SortedSet()
        {
            var persons = new[]
            {
                new Person {
                    Ssn = "324-00-3015", DateOfBirth = new DateTime(1970, 12, 17), FirstName = "Tod", LastName = "Temme"
                },
                new Person {
                    Ssn = "548-00-1592", DateOfBirth = new DateTime(1968, 03, 13), FirstName = "Lucia", LastName = "Armstrong"
                },
                new Person {
                    Ssn = "129-00-7416", DateOfBirth = new DateTime(1982, 09, 02), FirstName = "Spencer", LastName = "Weaver"
                },
                new Person {
                    Ssn = "831-00-6391", DateOfBirth = new DateTime(1974, 04, 30), FirstName = "Celia", LastName = "Potter"
                },
                new Person {
                    Ssn = "714-00-6502", DateOfBirth = new DateTime(1966, 11, 19), FirstName = "Powell", LastName = "Beck"
                },
            };

            var bySsn = new SortedSet <Person>(persons, KeyComparer.Create(
                                                   (Person p) => p.Ssn));

            // Not suitable for real-world scenarios, since SortedSet<T> does not allow duplicates.
            var byDateOfBirth = new SortedSet <Person>(persons, KeyComparer.Create(
                                                           (Person p) => p.DateOfBirth));
            var byFullName = new SortedSet <Person>(persons, KeyComparer.Create(
                                                        (Person p) => $"{p.FirstName} {p.LastName}", StringComparer.Ordinal));

            EnumerableAssert.AreEqual(new[] { "Spencer", "Tod", "Lucia", "Powell", "Celia" }, bySsn.Select(p => p.FirstName));
            EnumerableAssert.AreEqual(new[] { "Powell", "Lucia", "Tod", "Celia", "Spencer" }, byDateOfBirth.Select(p => p.FirstName));
            EnumerableAssert.AreEqual(new[] { "Celia", "Lucia", "Powell", "Spencer", "Tod" }, byFullName.Select(p => p.FirstName));

            var person = new Person {
                Ssn = "301-00-1582", DateOfBirth = new DateTime(1984, 11, 01), FirstName = "Teddy", LastName = "Wake"
            };

            bySsn.Add(person);
            EnumerableAssert.AreEqual(new[] { "Spencer", "Teddy", "Tod", "Lucia", "Powell", "Celia" }, bySsn.Select(p => p.FirstName));
        }
示例#7
0
 public static IEnumerable <T> DistinctBy <T, K>(this IEnumerable <T> input, Func <T, K> keySelector)
 {
     return(input.Distinct(KeyComparer.Create(keySelector)));
 }
示例#8
0
 public static IComparer <TElement> ThenByDescending <TElement, TKey>(this IComparer <TElement> primaryComparer, Func <TElement, TKey> keySelector, IComparer <TKey> keyComparer)
 {
     return(primaryComparer.Then(KeyComparer.Create(keySelector, keyComparer).Descending()));
 }
示例#9
0
 public static IComparer <TElement> ThenBy <TElement, TKey>(this IComparer <TElement> primaryComparer, Func <TElement, TKey> keySelector)
 {
     return(primaryComparer.Then(KeyComparer.Create(keySelector)));
 }