Exemplo n.º 1
0
            private void InferBaseType(
                TService service,
                SemanticDocument document,
                CancellationToken cancellationToken)
            {
                // See if we can find a possible base type for the type being generated.
                // NOTE(cyrusn): I currently limit this to when we have an object creation node.
                // That's because that's when we would have an expression that could be converted to
                // something else.  i.e. if the user writes "IList<int> list = new Goo()" then we can
                // infer a base interface for 'Goo'.  However, if they write "IList<int> list = Goo"
                // then we don't really want to infer a base type for 'Goo'.

                // However, there are a few other cases were we can infer a base type.
                var syntaxFacts = document.Project.LanguageServices.GetService <ISyntaxFactsService>();

                if (service.IsInCatchDeclaration(this.NameOrMemberAccessExpression))
                {
                    this.BaseTypeOrInterfaceOpt = document.SemanticModel.Compilation.ExceptionType();
                }
                else if (syntaxFacts.IsAttributeName(this.NameOrMemberAccessExpression))
                {
                    this.BaseTypeOrInterfaceOpt = document.SemanticModel.Compilation.AttributeType();
                }
                else if (
                    service.IsArrayElementType(this.NameOrMemberAccessExpression) ||
                    service.IsInVariableTypeContext(this.NameOrMemberAccessExpression) ||
                    this.ObjectCreationExpressionOpt != null)
                {
                    var expr          = this.ObjectCreationExpressionOpt ?? this.NameOrMemberAccessExpression;
                    var typeInference = document.Project.LanguageServices.GetService <ITypeInferenceService>();
                    var baseType      = typeInference.InferType(document.SemanticModel, expr, objectAsDefault: true, cancellationToken: cancellationToken) as INamedTypeSymbol;
                    SetBaseType(baseType);
                }
            }