Пример #1
0
            private async Task DetermineNamespaceOrTypeToGenerateInAsync(
                TService service,
                SemanticDocument document,
                CancellationToken cancellationToken)
            {
                DetermineNamespaceOrTypeToGenerateInWorker(service, document.SemanticModel, cancellationToken);

                // Can only generate into a type if it's a class and it's from source.
                if (this.TypeToGenerateInOpt != null)
                {
                    if (this.TypeToGenerateInOpt.TypeKind != TypeKind.Class &&
                        this.TypeToGenerateInOpt.TypeKind != TypeKind.Module)
                    {
                        this.TypeToGenerateInOpt = null;
                    }
                    else
                    {
                        var symbol = await SymbolFinder.FindSourceDefinitionAsync(this.TypeToGenerateInOpt, document.Project.Solution, cancellationToken).ConfigureAwait(false);

                        if (symbol == null ||
                            !symbol.IsKind(SymbolKind.NamedType) ||
                            !symbol.Locations.Any(loc => loc.IsInSource))
                        {
                            this.TypeToGenerateInOpt = null;
                            return;
                        }

                        var sourceTreeToBeGeneratedIn = symbol.Locations.First(loc => loc.IsInSource).SourceTree;
                        var documentToBeGeneratedIn   = document.Project.Solution.GetDocument(sourceTreeToBeGeneratedIn);

                        if (documentToBeGeneratedIn == null)
                        {
                            this.TypeToGenerateInOpt = null;
                            return;
                        }

                        // If the 2 documents are in different project then we must have Public Accessibility.
                        // If we are generating in a website project, we also want to type to be public so the
                        // designer files can access the type.
                        if (documentToBeGeneratedIn.Project != document.Project ||
                            service.GeneratedTypesMustBePublic(documentToBeGeneratedIn.Project))
                        {
                            this.IsPublicAccessibilityForTypeGeneration = true;
                        }

                        this.TypeToGenerateInOpt = (INamedTypeSymbol)symbol;
                    }
                }

                if (this.TypeToGenerateInOpt != null)
                {
                    if (!CodeGenerator.CanAdd(document.Project.Solution, this.TypeToGenerateInOpt, cancellationToken))
                    {
                        this.TypeToGenerateInOpt = null;
                    }
                }
            }