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


C# ExpressionSyntax.CopyAnnotationsTo方法代码示例

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


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

示例1: FullyQualifyIdentifierName

            private ExpressionSyntax FullyQualifyIdentifierName(
                ISymbol symbol,
                ExpressionSyntax rewrittenNode,
                ExpressionSyntax originalNode,
                bool replaceNode,
                bool isInsideCref,
                bool omitLeftHandSide)
            {
                Debug.Assert(!replaceNode || rewrittenNode.Kind() == SyntaxKind.IdentifierName);

                //// TODO: use and expand Generate*Syntax(isymbol) to not depend on symbol display any more.
                //// See GenerateExpressionSyntax();

                var result = rewrittenNode;

                // only if this symbol has a containing type or namespace there is work for us to do.
                if (replaceNode || symbol.ContainingType != null || symbol.ContainingNamespace != null)
                {
                    ImmutableArray<SymbolDisplayPart> displayParts;

                    ExpressionSyntax left = null;

                    // we either need to create an AliasQualifiedName if the symbol is directly contained in the global namespace,
                    // otherwise it a QualifiedName.
                    if (!replaceNode && symbol.ContainingType == null && symbol.ContainingNamespace.IsGlobalNamespace)
                    {
                        return rewrittenNode.CopyAnnotationsTo(
                            SyntaxFactory.AliasQualifiedName(
                                SyntaxFactory.IdentifierName(SyntaxFactory.Token(SyntaxKind.GlobalKeyword)),
                                (SimpleNameSyntax)rewrittenNode.WithLeadingTrivia(null))
                                    .WithLeadingTrivia(rewrittenNode.GetLeadingTrivia()));
                    }

                    displayParts = replaceNode
                        ? symbol.ToDisplayParts(s_typeNameFormatWithGenerics)
                        : (symbol.ContainingType ?? (ISymbol)symbol.ContainingNamespace).ToDisplayParts(s_typeNameFormatWithGenerics);

                    rewrittenNode = TryAddTypeArgumentToIdentifierName(rewrittenNode, symbol);

                    // Replaces the '<' token with the '{' token since we are inside crefs
                    rewrittenNode = TryReplaceAngleBracesWithCurlyBraces(rewrittenNode, isInsideCref);
                    result = rewrittenNode;

                    if (!omitLeftHandSide)
                    {
                        left = SyntaxFactory.ParseTypeName(displayParts.ToDisplayString());

                        // Replaces the '<' token with the '{' token since we are inside crefs
                        left = TryReplaceAngleBracesWithCurlyBraces(left, isInsideCref);

                        if (replaceNode)
                        {
                            return left
                                .WithLeadingTrivia(rewrittenNode.GetLeadingTrivia())
                                .WithTrailingTrivia(rewrittenNode.GetTrailingTrivia());
                        }

                        // now create syntax for the combination of left and right syntax, or a simple replacement in case of an identifier
                        var parent = originalNode.Parent;
                        var leadingTrivia = rewrittenNode.GetLeadingTrivia();
                        rewrittenNode = rewrittenNode.WithLeadingTrivia(null);

                        switch (parent.Kind())
                        {
                            case SyntaxKind.QualifiedName:
                                var qualifiedParent = (QualifiedNameSyntax)parent;

                                result = rewrittenNode.CopyAnnotationsTo(
                                    SyntaxFactory.QualifiedName(
                                        (NameSyntax)left,
                                        (SimpleNameSyntax)rewrittenNode));

                                break;

                            case SyntaxKind.SimpleMemberAccessExpression:
                                var memberAccessParent = (MemberAccessExpressionSyntax)parent;

                                result = rewrittenNode.CopyAnnotationsTo(
                                    SyntaxFactory.MemberAccessExpression(
                                        SyntaxKind.SimpleMemberAccessExpression,
                                        left,
                                        (SimpleNameSyntax)rewrittenNode));

                                break;

                            default:
                                Debug.Assert(rewrittenNode is SimpleNameSyntax);

                                if (SyntaxFacts.IsInNamespaceOrTypeContext(originalNode))
                                {
                                    var right = (SimpleNameSyntax)rewrittenNode;
                                    result = rewrittenNode.CopyAnnotationsTo(SyntaxFactory.QualifiedName((NameSyntax)left, right.WithAdditionalAnnotations(Simplifier.SpecialTypeAnnotation)));
                                }
                                else if (originalNode.Parent is CrefSyntax)
                                {
                                    var right = (SimpleNameSyntax)rewrittenNode;
                                    result = rewrittenNode.CopyAnnotationsTo(SyntaxFactory.QualifiedName((NameSyntax)left, right));
                                }
                                else
                                {
//.........这里部分代码省略.........
开发者ID:RoryVL,项目名称:roslyn,代码行数:101,代码来源:CSharpSimplificationService.Expander.cs

示例2: TryAddTypeArgumentToIdentifierName

            private ExpressionSyntax TryAddTypeArgumentToIdentifierName(ExpressionSyntax newNode, ISymbol symbol)
            {
                if (newNode.Kind() == SyntaxKind.IdentifierName && symbol.Kind == SymbolKind.Method)
                {
                    if (((IMethodSymbol)symbol).TypeArguments.Length != 0)
                    {
                        var typeArguments = ((IMethodSymbol)symbol).TypeArguments;
                        if (!typeArguments.Any(t => t.ContainsAnonymousType()))
                        {
                            var genericName = SyntaxFactory.GenericName(
                                            ((IdentifierNameSyntax)newNode).Identifier,
                                            SyntaxFactory.TypeArgumentList(
                                                SyntaxFactory.SeparatedList(
                                                    typeArguments.Select(p => SyntaxFactory.ParseTypeName(p.ToDisplayParts(s_typeNameFormatWithGenerics).ToDisplayString())))))
                                            .WithLeadingTrivia(newNode.GetLeadingTrivia())
                                            .WithTrailingTrivia(newNode.GetTrailingTrivia())
                                            .WithAdditionalAnnotations(Simplifier.Annotation);

                            genericName = newNode.CopyAnnotationsTo(genericName);
                            return genericName;
                        }
                    }
                }

                return newNode;
            }
开发者ID:RoryVL,项目名称:roslyn,代码行数:26,代码来源:CSharpSimplificationService.Expander.cs


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