Exemplo n.º 1
0
 public SolutionServices(Workspace workspace, IWorkspaceServiceProvider workspaceServices)
 {
     this.Workspace                 = workspace;
     this.WorkspaceServices         = workspaceServices;
     this.LanguageServicesFactory   = WorkspaceServices.GetService <ILanguageServiceProviderFactory>();
     this.TemporaryStorage          = WorkspaceServices.GetService <ITemporaryStorageService>();
     this.TextFactory               = WorkspaceServices.GetService <ITextFactoryService>();
     this.TextCache                 = WorkspaceServices.GetService <ITextCacheService>();
     this.CompilationCacheService   = WorkspaceServices.GetService <ICompilationCacheService>();
     this.MetadataReferenceProvider = WorkspaceServices.GetService <IMetadataReferenceProviderService>().GetProvider();
 }
Exemplo n.º 2
0
 public SolutionServices(Workspace workspace, IWorkspaceServiceProvider workspaceServices)
 {
     this.Workspace = workspace;
     this.WorkspaceServices = workspaceServices;
     this.LanguageServicesFactory = WorkspaceServices.GetService<ILanguageServiceProviderFactory>();
     this.TemporaryStorage = WorkspaceServices.GetService<ITemporaryStorageService>();
     this.TextFactory = WorkspaceServices.GetService<ITextFactoryService>();
     this.TextCache = WorkspaceServices.GetService<ITextCacheService>();
     this.CompilationCacheService = WorkspaceServices.GetService<ICompilationCacheService>();
     this.MetadataReferenceProvider = WorkspaceServices.GetService<IMetadataReferenceProviderService>().GetProvider();
 }
        private static ISymbol FindImplementations <TSymbol>(
            ITypeSymbol typeSymbol,
            TSymbol interfaceSymbol,
            ILanguageServiceProviderFactory languageServiceProviderFactory,
            Func <TSymbol, ImmutableArray <TSymbol> > getExplicitInterfaceImplementations) where TSymbol : class, ISymbol
        {
            // Check the current type for explicit interface matches.  Otherwise, check
            // the current type and base types for implicit matches.
            var explicitMatches =
                from member in typeSymbol.GetMembers().OfType <TSymbol>()
                where getExplicitInterfaceImplementations(member).Length > 0
                from explicitInterfaceMethod in getExplicitInterfaceImplementations(member)
                where SymbolEquivalenceComparer.Instance.Equals(explicitInterfaceMethod, interfaceSymbol)
                select member;

            var provider      = languageServiceProviderFactory.GetLanguageServiceProvider(typeSymbol.Language);
            var semanticFacts = provider.GetService <ISemanticFactsService>();

            // Even if a language only supports explicit interface implementation, we
            // can't enforce it for types from metadata. For example, a VB symbol
            // representing System.Xml.XmlReader will say it implements IDisposable, but
            // the XmlReader.Dispose() method will not be an explicit implementation of
            // IDisposable.Dispose()
            if (!semanticFacts.SupportsImplicitInterfaceImplementation &&
                typeSymbol.Locations.Any(location => location.IsInSource))
            {
                return(explicitMatches.FirstOrDefault());
            }

            var syntaxFacts     = provider.GetService <ISyntaxFactsService>();
            var implicitMatches =
                from baseType in typeSymbol.GetBaseTypesAndThis()
                from member in baseType.GetMembers(interfaceSymbol.Name).OfType <TSymbol>()
                where member.DeclaredAccessibility == Accessibility.Public &&
                !member.IsStatic &&
                SignatureComparer.Instance.HaveSameSignatureAndConstraintsAndReturnTypeAndAccessors(member, interfaceSymbol, syntaxFacts.IsCaseSensitive)
                select member;

            return(explicitMatches.FirstOrDefault() ?? implicitMatches.FirstOrDefault());
        }
 public GenerateFieldOrPropertyCodeIssueProvider(
     ILanguageServiceProviderFactory languageServiceProviderFactory,
     ICodeDefinitionFactory codeDefinitionFactory)
     : base(languageServiceProviderFactory, codeDefinitionFactory)
 {
 }
        /// <summary>
        /// Returns the corresponding symbol in this type or a base type that implements
        /// interfaceMember (either implicitly or explicitly), or null if no such symbol exists
        /// (which might be either because this type doesn't implement the container of
        /// interfaceMember, or this type doesn't supply a member that successfully implements
        /// interfaceMember).
        /// </summary>
        public static IEnumerable <ISymbol> FindImplementationsForInterfaceMember(
            this ITypeSymbol typeSymbol,
            ISymbol interfaceMember,
            ILanguageServiceProviderFactory languageServiceProviderFactory,
            CancellationToken cancellationToken)
        {
            // This method can return multiple results.  Consider the case of:
            //
            // interface IFoo<X> { void Foo(X x); }
            //
            // class C : IFoo<int>, IFoo<string> { void Foo(int x); void Foo(string x); }
            //
            // If you're looking for the implementations of IFoo<X>.Foo then you want to find both
            // results in C.

            // TODO(cyrusn): Implement this using the actual code for
            // TypeSymbol.FindImplementationForInterfaceMember

            if (typeSymbol == null || interfaceMember == null)
            {
                yield break;
            }

            if (interfaceMember.Kind != SymbolKind.Event &&
                interfaceMember.Kind != SymbolKind.Method &&
                interfaceMember.Kind != SymbolKind.Property)
            {
                yield break;
            }

            // WorkItem(4843)
            //
            // 'typeSymbol' has to at least implement the interface containing the member.  note:
            // this just means that the interface shows up *somewhere* in the inheritance chain of
            // this type.  However, this type may not actually say that it implements it.  For
            // example:
            //
            // interface I { void Foo(); }
            //
            // class B { }
            //
            // class C : B, I { }
            //
            // class D : C { }
            //
            // D does implement I transitively through C.  However, even if D has a "Foo" method, it
            // won't be an implementation of I.Foo.  The implementation of I.Foo must be from a type
            // that actually has I in it's direct interface chain, or a type that's a base type of
            // that.  in this case, that means only classes C or B.
            var interfaceType = interfaceMember.ContainingType;

            if (!typeSymbol.ImplementsIgnoringConstruction(interfaceType))
            {
                yield break;
            }

            // We've ascertained that the type T implements some constructed type of the form I<X>.
            // However, we're not precisely sure which constructions of I<X> are being used.  For
            // example, a type C might implement I<int> and I<string>.  If we're searching for a
            // method from I<X> we might need to find several methods that implement different
            // instantiations of that method.
            var originalInterfaceType   = interfaceMember.ContainingType.OriginalDefinition;
            var originalInterfaceMember = interfaceMember.OriginalDefinition;
            var constructedInterfaces   = typeSymbol.AllInterfaces.Where(i =>
                                                                         SymbolEquivalenceComparer.Instance.Equals(i.OriginalDefinition, originalInterfaceType));

            foreach (var constructedInterface in constructedInterfaces)
            {
                cancellationToken.ThrowIfCancellationRequested();
                var constructedInterfaceMember = constructedInterface.GetMembers().FirstOrDefault(m =>
                                                                                                  SymbolEquivalenceComparer.Instance.Equals(m.OriginalDefinition, originalInterfaceMember));

                if (constructedInterfaceMember == null)
                {
                    continue;
                }

                // Now we need to walk the base type chain, but we start at the first type that actually
                // has the interface directly in its interface hierarchy.
                var seenTypeDeclaringInterface = false;
                for (var currentType = typeSymbol; currentType != null; currentType = currentType.BaseType)
                {
                    seenTypeDeclaringInterface = seenTypeDeclaringInterface ||
                                                 currentType.GetOriginalInterfacesAndTheirBaseInterfaces().Contains(interfaceType.OriginalDefinition);

                    if (seenTypeDeclaringInterface)
                    {
                        var result = constructedInterfaceMember.TypeSwitch(
                            (IEventSymbol eventSymbol) => FindImplementations(currentType, eventSymbol, languageServiceProviderFactory, e => e.ExplicitInterfaceImplementations),
                            (IMethodSymbol methodSymbol) => FindImplementations(currentType, methodSymbol, languageServiceProviderFactory, m => m.ExplicitInterfaceImplementations),
                            (IPropertySymbol propertySymbol) => FindImplementations(currentType, propertySymbol, languageServiceProviderFactory, p => p.ExplicitInterfaceImplementations));

                        if (result != null)
                        {
                            yield return(result);

                            break;
                        }
                    }
                }
            }
        }
 public GenerateFieldOrPropertyCodeIssueProvider(
     ILanguageServiceProviderFactory languageServiceProviderFactory,
     ICodeDefinitionFactory codeDefinitionFactory)
     : base(languageServiceProviderFactory, codeDefinitionFactory)
 {
 }
 public static TService GetService <TService>(this ILanguageServiceProviderFactory factory, string language)
     where TService : ILanguageService
 {
     return(factory.GetLanguageServiceProvider(language).GetService <TService>());
 }
 public CSharpGenerateFieldOrPropertyService(
     ILanguageServiceProviderFactory languageServiceProviderFactory,
     ICodeDefinitionFactory codeDefinitionFactory)
     : base(languageServiceProviderFactory, codeDefinitionFactory)
 {
 }