Exemple #1
0
        /// <inheritdoc cref="FindImplementationsAsync(INamedTypeSymbol, Solution, IImmutableSet{Project}, CancellationToken)"/>
        /// <remarks>
        /// Use this overload to avoid boxing the result into an <see cref="IEnumerable{T}"/>.
        /// </remarks>
        internal static async Task <ImmutableArray <INamedTypeSymbol> > FindImplementationsArrayAsync(
            INamedTypeSymbol type, Solution solution, bool transitive, IImmutableSet <Project> projects = null, CancellationToken cancellationToken = default)
        {
            var types = await DependentTypeFinder.FindImplementingTypesAsync(
                type, solution, projects, transitive, cancellationToken).ConfigureAwait(false);

            return(types.WhereAsArray(t => IsAccessible(t)));
        }
        /// <summary>
        /// Finds the symbols that implement an interface or interface member.
        /// </summary>
        public static async Task <IEnumerable <ISymbol> > FindImplementationsAsync(
            ISymbol symbol, Solution solution, IImmutableSet <Project> projects = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            // A symbol can only have implementations if it's an interface or a
            // method/property/event from an interface.
            if (symbol is INamedTypeSymbol)
            {
                var namedTypeSymbol   = (INamedTypeSymbol)symbol;
                var implementingTypes = await DependentTypeFinder.FindImplementingTypesAsync(namedTypeSymbol, solution, projects, cancellationToken).ConfigureAwait(false);

                return(implementingTypes.Where(IsAccessible));
            }
            else if (symbol.IsImplementableMember())
            {
                var containingType = symbol.ContainingType.OriginalDefinition;
                var allTypes       = await DependentTypeFinder.FindImplementingTypesAsync(containingType, solution, projects, cancellationToken).ConfigureAwait(false);

                List <ISymbol> results = null;
                foreach (var t in allTypes)
                {
                    foreach (var m in t.FindImplementationsForInterfaceMember(symbol, solution.Workspace, cancellationToken))
                    {
                        var s = await FindSourceDefinitionAsync(m, solution, cancellationToken).ConfigureAwait(false) ?? m;

                        if (IsAccessible(s))
                        {
                            results = results ?? new List <ISymbol>();
                            results.Add(s.OriginalDefinition);
                        }
                    }
                }

                if (results != null)
                {
                    return(results.Distinct(SymbolEquivalenceComparer.Instance));
                }
            }

            return(SpecializedCollections.EmptyEnumerable <ISymbol>());
        }