Ejemplo n.º 1
0
        internal override void GetCandidateExtensionMethods(
            ArrayBuilder <MethodSymbol> methods,
            string name,
            int arity,
            LookupOptions options,
            Binder originalBinder)
        {
            Debug.Assert(methods.Count == 0);

            bool callerIsSemanticModel = originalBinder.IsSemanticModelBinder;

            // We need to avoid collecting multiple candidates for an extension method imported both through a namespace and a static class
            // We will look for duplicates only if both of the following flags are set to true
            bool seenNamespaceWithExtensionMethods   = false;
            bool seenStaticClassWithExtensionMethods = false;

            foreach (var nsOrType in this.GetUsings(basesBeingResolved: null))
            {
                switch (nsOrType.NamespaceOrType.Kind)
                {
                case SymbolKind.Namespace:
                {
                    var count = methods.Count;
                    ((NamespaceSymbol)nsOrType.NamespaceOrType).GetExtensionMethods(methods, name, arity, options);

                    // If we found any extension methods, then consider this using as used.
                    if (methods.Count != count)
                    {
                        MarkImportDirective(nsOrType.UsingDirectiveReference, callerIsSemanticModel);
                        seenNamespaceWithExtensionMethods = true;
                    }

                    break;
                }

                case SymbolKind.NamedType:
                {
                    var count = methods.Count;
                    ((NamedTypeSymbol)nsOrType.NamespaceOrType).GetExtensionMethods(methods, name, arity, options);

                    // If we found any extension methods, then consider this using as used.
                    if (methods.Count != count)
                    {
                        MarkImportDirective(nsOrType.UsingDirectiveReference, callerIsSemanticModel);
                        seenStaticClassWithExtensionMethods = true;
                    }

                    break;
                }
                }
            }

            if (seenNamespaceWithExtensionMethods && seenStaticClassWithExtensionMethods)
            {
                methods.RemoveDuplicates();
            }
        }
Ejemplo n.º 2
0
        public void RemoveDuplicates1()
        {
            var builder = new ArrayBuilder<int> { 1, 2, 3, 2, 4, 5, 1 };
            builder.RemoveDuplicates();
            AssertEx.Equal(new[] { 1, 2, 3, 4, 5 }, builder);

            builder = new ArrayBuilder<int> { 1 };
            builder.RemoveDuplicates();
            AssertEx.Equal(new[] { 1 }, builder);

            builder = new ArrayBuilder<int>();
            builder.RemoveDuplicates();
            AssertEx.Equal(new int[0], builder);
        }
Ejemplo n.º 3
0
        public void RemoveDuplicates1()
        {
            var builder = new ArrayBuilder <int> {
                1, 2, 3, 2, 4, 5, 1
            };

            builder.RemoveDuplicates();
            AssertEx.Equal(new[] { 1, 2, 3, 4, 5 }, builder);

            builder = new ArrayBuilder <int> {
                1
            };
            builder.RemoveDuplicates();
            AssertEx.Equal(new[] { 1 }, builder);

            builder = new ArrayBuilder <int>();
            builder.RemoveDuplicates();
            AssertEx.Equal(new int[0], builder);
        }
Ejemplo n.º 4
0
        internal void LookupExtensionMethodsInUsings(
            ArrayBuilder<MethodSymbol> methods,
            string name,
            int arity,
            LookupOptions options,
            bool callerIsSemanticModel)
        {
            Debug.Assert(methods.Count == 0);

            // We need to avoid collecting multiple candidates for an extension method imported both through a namespace and a static class
            // We will look for duplicates only if both of the following flags are set to true
            bool seenNamespaceWithExtensionMethods = false;
            bool seenStaticClassWithExtensionMethods = false;

            foreach (var nsOrType in this.Usings)
            {
                switch (nsOrType.NamespaceOrType.Kind)
                {
                    case SymbolKind.Namespace:
                        {
                            var count = methods.Count;
                            ((NamespaceSymbol)nsOrType.NamespaceOrType).GetExtensionMethods(methods, name, arity, options);

                            // If we found any extension methods, then consider this using as used.
                            if (methods.Count != count)
                            {
                                MarkImportDirective(nsOrType.UsingDirective, callerIsSemanticModel);
                                seenNamespaceWithExtensionMethods = true;
                            }

                            break;
                        }

                    case SymbolKind.NamedType:
                        {
                            var count = methods.Count;
                            ((NamedTypeSymbol)nsOrType.NamespaceOrType).GetExtensionMethods(methods, name, arity, options);

                            // If we found any extension methods, then consider this using as used.
                            if (methods.Count != count)
                            {
                                MarkImportDirective(nsOrType.UsingDirective, callerIsSemanticModel);
                                seenStaticClassWithExtensionMethods = true;
                            }

                            break;
                        }
                }
            }

            if (seenNamespaceWithExtensionMethods && seenStaticClassWithExtensionMethods)
            {
                methods.RemoveDuplicates();
            }
        }