当前位置: 首页>>代码示例>>C#>>正文


C# SemanticModel.GetSpeculativeSymbolInfo方法代码示例

本文整理汇总了C#中SemanticModel.GetSpeculativeSymbolInfo方法的典型用法代码示例。如果您正苦于以下问题:C# SemanticModel.GetSpeculativeSymbolInfo方法的具体用法?C# SemanticModel.GetSpeculativeSymbolInfo怎么用?C# SemanticModel.GetSpeculativeSymbolInfo使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SemanticModel的用法示例。


在下文中一共展示了SemanticModel.GetSpeculativeSymbolInfo方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CanAccessInstanceAndStaticMembersOffOf

        public static bool CanAccessInstanceAndStaticMembersOffOf(
            this ExpressionSyntax expression,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            // Check for the Color Color case.
            //
            // color color: if you bind "A" and you get a symbol and the type of that symbol is
            // Q; and if you bind "A" *again* as a type and you get type Q, then both A.static
            // and A.instance are permitted
            if (expression is IdentifierNameSyntax)
            {
                var instanceSymbol = semanticModel.GetSymbolInfo(expression, cancellationToken).GetAnySymbol();

                if (!(instanceSymbol is INamespaceOrTypeSymbol))
                {
                    var instanceType = instanceSymbol.GetSymbolType();
                    if (instanceType != null)
                    {
                        var speculativeSymbolInfo = semanticModel.GetSpeculativeSymbolInfo(expression.SpanStart, expression, SpeculativeBindingOption.BindAsTypeOrNamespace);
                        if (speculativeSymbolInfo.CandidateReason != CandidateReason.NotATypeOrNamespace)
                        {
                            var staticType = speculativeSymbolInfo.GetAnySymbol().GetSymbolType();

                            return SymbolEquivalenceComparer.Instance.Equals(instanceType, staticType);
                        }
                    }
                }
            }

            return false;
        }
开发者ID:nileshjagtap,项目名称:roslyn,代码行数:32,代码来源:ExpressionSyntaxExtensions.cs

示例2: TryReduceExtensionMethod

        private static InvocationExpressionSyntax TryReduceExtensionMethod(InvocationExpressionSyntax node, SemanticModel semanticModel, InvocationExpressionSyntax rewrittenNode, SimpleNameSyntax expressionName)
        {
            var targetSymbol = semanticModel.GetSymbolInfo(expressionName);

            if (targetSymbol.Symbol != null && targetSymbol.Symbol.Kind == SymbolKind.Method)
            {
                var targetMethodSymbol = (IMethodSymbol)targetSymbol.Symbol;
                if (!targetMethodSymbol.IsReducedExtension())
                {
                    var argumentList = node.ArgumentList;
                    var noOfArguments = argumentList.Arguments.Count;

                    if (noOfArguments > 0)
                    {
                        MemberAccessExpressionSyntax newMemberAccess = null;
                        var invocationExpressionNodeExpression = node.Expression;

                        if (node.Expression.CSharpKind() == SyntaxKind.SimpleMemberAccessExpression)
                        {
                            newMemberAccess = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, argumentList.Arguments.ElementAt(0).Expression, ((MemberAccessExpressionSyntax)invocationExpressionNodeExpression).OperatorToken, ((MemberAccessExpressionSyntax)invocationExpressionNodeExpression).Name);
                        }
                        else if (node.Expression.CSharpKind() == SyntaxKind.IdentifierName)
                        {
                            newMemberAccess = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, argumentList.Arguments.ElementAt(0).Expression, (IdentifierNameSyntax)invocationExpressionNodeExpression.WithLeadingTrivia());
                        }
                        else if (node.Expression.CSharpKind() == SyntaxKind.GenericName)
                        {
                            newMemberAccess = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, argumentList.Arguments.ElementAt(0).Expression, (GenericNameSyntax)invocationExpressionNodeExpression.WithLeadingTrivia());
                        }
                        else
                        {
                            Debug.Assert(false, "The expression kind is not MemberAccessExpression or IdentifierName or GenericName to be converted to Member Access Expression for Ext Method Reduction");
                        }

                        if (newMemberAccess == null)
                        {
                            return node;
                        }

                        // Preserve Trivia
                        newMemberAccess = newMemberAccess.WithLeadingTrivia(node.GetLeadingTrivia());

                        // Below removes the first argument
                        // we need to reuse the separators to maintain existing formatting & comments in the arguments itself
                        var newArguments = SyntaxFactory.SeparatedList<ArgumentSyntax>(argumentList.Arguments.GetWithSeparators().AsEnumerable().Skip(2));

                        var rewrittenArgumentList = argumentList.WithArguments(newArguments);
                        var candidateRewrittenNode = SyntaxFactory.InvocationExpression(newMemberAccess, rewrittenArgumentList);

                        var oldSymbol = semanticModel.GetSymbolInfo(node).Symbol;
                        var newSymbol = semanticModel.GetSpeculativeSymbolInfo(
                            node.SpanStart,
                            candidateRewrittenNode,
                            SpeculativeBindingOption.BindAsExpression).Symbol;

                        if (oldSymbol != null && newSymbol != null)
                        {
                            if (newSymbol.Kind == SymbolKind.Method && oldSymbol.Equals(((IMethodSymbol)newSymbol).GetConstructedReducedFrom()))
                            {
                                rewrittenNode = candidateRewrittenNode;
                            }
                        }
                    }
                }
            }

            return rewrittenNode;
        }
开发者ID:modulexcite,项目名称:pattern-matching-csharp,代码行数:68,代码来源:CSharpExtensionMethodReducer.cs

示例3: IsReplaceableByVar

        private static bool IsReplaceableByVar(
            this TypeSyntax simpleName,
            SemanticModel semanticModel,
            out TypeSyntax replacementNode,
            out TextSpan issueSpan,
            OptionSet optionSet,
            CancellationToken cancellationToken)
        {
            replacementNode = null;
            issueSpan = default(TextSpan);

            if (!optionSet.GetOption(SimplificationOptions.PreferImplicitTypeInLocalDeclaration))
            {
                return false;
            }

            // If it is already var
            if (simpleName.IsVar)
            {
                return false;
            }

            var candidateReplacementNode = SyntaxFactory.IdentifierName("var")
                                            .WithLeadingTrivia(simpleName.GetLeadingTrivia())
                                            .WithTrailingTrivia(simpleName.GetTrailingTrivia());
            var candidateIssueSpan = simpleName.Span;

            // If there exists a Type called var , fail.
            var checkSymbol = semanticModel.GetSpeculativeSymbolInfo(simpleName.SpanStart, candidateReplacementNode, SpeculativeBindingOption.BindAsTypeOrNamespace).Symbol;
            if (checkSymbol != null && checkSymbol.IsKind(SymbolKind.NamedType) && ((INamedTypeSymbol)checkSymbol).TypeKind == TypeKind.Class && checkSymbol.Name == "var")
            {
                return false;
            }

            // If the simpleName is the type of the Variable Declaration Syntax belonging to LocalDeclaration, For Statement or Using statement
            if (simpleName.IsParentKind(SyntaxKind.VariableDeclaration) &&
                ((VariableDeclarationSyntax)simpleName.Parent).Type == simpleName &&
                simpleName.Parent.Parent.IsKind(SyntaxKind.LocalDeclarationStatement, SyntaxKind.ForStatement, SyntaxKind.UsingStatement))
            {
                if (simpleName.Parent.IsParentKind(SyntaxKind.LocalDeclarationStatement) &&
                    ((LocalDeclarationStatementSyntax)simpleName.Parent.Parent).Modifiers.Any(n => n.Kind() == SyntaxKind.ConstKeyword))
                {
                    return false;
                }

                var variableDeclaration = (VariableDeclarationSyntax)simpleName.Parent;

                // Check the Initialized Value to see if it is allowed to be in the Var initialization
                if (variableDeclaration.Variables.Count != 1 ||
                    !variableDeclaration.Variables.Single().Initializer.IsKind(SyntaxKind.EqualsValueClause))
                {
                    return false;
                }

                var variable = variableDeclaration.Variables.Single();
                var initializer = variable.Initializer;
                var identifier = variable.Identifier;

                if (EqualsValueClauseNotSuitableForVar(identifier, simpleName, initializer, semanticModel, cancellationToken))
                {
                    return false;
                }

                replacementNode = candidateReplacementNode;
                issueSpan = candidateIssueSpan;
                return true;
            }

            if (simpleName.IsParentKind(SyntaxKind.ForEachStatement) &&
                ((ForEachStatementSyntax)simpleName.Parent).Type == simpleName)
            {
                replacementNode = candidateReplacementNode;
                issueSpan = candidateIssueSpan;
                return true;
            }

            return false;
        }
开发者ID:nileshjagtap,项目名称:roslyn,代码行数:78,代码来源:ExpressionSyntaxExtensions.cs

示例4: TryReduceOrSimplifyExplicitName

        public static bool TryReduceOrSimplifyExplicitName(
            this CrefSyntax crefSyntax,
            SemanticModel semanticModel,
            out CrefSyntax replacementNode,
            out TextSpan issueSpan,
            OptionSet optionSet,
            CancellationToken cancellationToken)
        {
            replacementNode = null;
            issueSpan = default(TextSpan);

            // Currently Qualified Cref is the only CrefSyntax We are handling separately
            if (crefSyntax.Kind() != SyntaxKind.QualifiedCref)
            {
                return false;
            }

            var qualifiedCrefSyntax = (QualifiedCrefSyntax)crefSyntax;
            var memberCref = qualifiedCrefSyntax.Member;

            // Currently we are dealing with only the NameMemberCrefs
            if (optionSet.GetOption(SimplificationOptions.PreferIntrinsicPredefinedTypeKeywordInMemberAccess, LanguageNames.CSharp) &&
                (memberCref.Kind() == SyntaxKind.NameMemberCref))
            {
                var nameMemberCref = ((NameMemberCrefSyntax)memberCref).Name;
                var symbolInfo = semanticModel.GetSymbolInfo(nameMemberCref, cancellationToken);
                var symbol = symbolInfo.Symbol;

                if (symbol == null)
                {
                    return false;
                }

                if (symbol is INamespaceOrTypeSymbol)
                {
                    var namespaceOrTypeSymbol = (INamespaceOrTypeSymbol)symbol;

                    // 1. Check for Predefined Types
                    if (symbol is INamedTypeSymbol)
                    {
                        var namedSymbol = (INamedTypeSymbol)symbol;
                        var keywordKind = ExpressionSyntaxExtensions.GetPredefinedKeywordKind(namedSymbol.SpecialType);

                        if (keywordKind != SyntaxKind.None)
                        {
                            replacementNode = SyntaxFactory.TypeCref(
                                                SyntaxFactory.PredefinedType(
                                                    SyntaxFactory.Token(crefSyntax.GetLeadingTrivia(), keywordKind, crefSyntax.GetTrailingTrivia())));
                            replacementNode = crefSyntax.CopyAnnotationsTo(replacementNode);

                            // we want to show the whole name expression as unnecessary
                            issueSpan = crefSyntax.Span;

                            return true;
                        }
                    }
                }
            }

            var oldSymbol = semanticModel.GetSymbolInfo(crefSyntax, cancellationToken).Symbol;
            if (oldSymbol != null)
            {
                var speculativeBindingOption = SpeculativeBindingOption.BindAsExpression;
                if (oldSymbol is INamespaceOrTypeSymbol)
                {
                    speculativeBindingOption = SpeculativeBindingOption.BindAsTypeOrNamespace;
                }

                var newSymbol = semanticModel.GetSpeculativeSymbolInfo(crefSyntax.SpanStart, memberCref, speculativeBindingOption).Symbol;

                if (newSymbol == oldSymbol)
                {
                    // Copy Trivia and Annotations
                    memberCref = memberCref.WithLeadingTrivia(crefSyntax.GetLeadingTrivia());
                    memberCref = crefSyntax.CopyAnnotationsTo(memberCref);
                    issueSpan = qualifiedCrefSyntax.Container.Span;
                    replacementNode = memberCref;
                    return true;
                }
            }

            return false;
        }
开发者ID:Rickinio,项目名称:roslyn,代码行数:83,代码来源:CrefSyntaxExtensions.cs


注:本文中的SemanticModel.GetSpeculativeSymbolInfo方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。