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


C# ExpressionSyntax.GetAncestorOrThis方法代码示例

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


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

示例1: IntroduceFieldAsync

        protected override Task<Tuple<Document, SyntaxNode, int>> IntroduceFieldAsync(
            SemanticDocument document,
            ExpressionSyntax expression,
            bool allOccurrences,
            bool isConstant,
            CancellationToken cancellationToken)
        {
            var oldTypeDeclaration = expression.GetAncestorOrThis<TypeDeclarationSyntax>();

            var oldType = oldTypeDeclaration != null
                ? document.SemanticModel.GetDeclaredSymbol(oldTypeDeclaration, cancellationToken) as INamedTypeSymbol
                : document.SemanticModel.Compilation.ScriptClass;
            var newNameToken = GenerateUniqueFieldName(document, expression, isConstant, cancellationToken);

            var newQualifiedName = oldTypeDeclaration != null
                ? SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.ParseName(oldType.ToNameDisplayString()), SyntaxFactory.IdentifierName(newNameToken))
                : (ExpressionSyntax)SyntaxFactory.IdentifierName(newNameToken);

            newQualifiedName = newQualifiedName.WithAdditionalAnnotations(Simplifier.Annotation);

            var newFieldDeclaration = SyntaxFactory.FieldDeclaration(
                default(SyntaxList<AttributeListSyntax>),
                MakeFieldModifiers(isConstant, inScript: oldType.IsScriptClass),
                SyntaxFactory.VariableDeclaration(
                    GetTypeSymbol(document, expression, cancellationToken).GenerateTypeSyntax(),
                    SyntaxFactory.SingletonSeparatedList(
                        SyntaxFactory.VariableDeclarator(
                            newNameToken.WithAdditionalAnnotations(RenameAnnotation.Create()),
                            null,
                            SyntaxFactory.EqualsValueClause(expression))))).WithAdditionalAnnotations(Formatter.Annotation);

            if (oldTypeDeclaration != null)
            {
                var newTypeDeclaration = Rewrite(
                    document, expression, newQualifiedName, document, oldTypeDeclaration, allOccurrences, cancellationToken);

                var insertionIndex = isConstant ?
                    DetermineConstantInsertPosition(oldTypeDeclaration.Members, newTypeDeclaration.Members) :
                    DetermineFieldInsertPosition(oldTypeDeclaration.Members, newTypeDeclaration.Members);

                var finalTypeDeclaration = InsertMember(newTypeDeclaration, newFieldDeclaration, insertionIndex);

                SyntaxNode destination = oldTypeDeclaration;
                var newRoot = document.Root.ReplaceNode(oldTypeDeclaration, finalTypeDeclaration);
                return Task.FromResult(Tuple.Create(document.Document.WithSyntaxRoot(newRoot), destination, insertionIndex));
            }
            else
            {
                var oldCompilationUnit = (CompilationUnitSyntax)document.Root;
                var newCompilationUnit = Rewrite(
                    document, expression, newQualifiedName, document, oldCompilationUnit, allOccurrences, cancellationToken);

                var insertionIndex = isConstant ?
                    DetermineConstantInsertPosition(oldCompilationUnit.Members, newCompilationUnit.Members) :
                    DetermineFieldInsertPosition(oldCompilationUnit.Members, newCompilationUnit.Members);

                SyntaxNode destination = oldCompilationUnit;
                var newRoot = newCompilationUnit.WithMembers(newCompilationUnit.Members.Insert(insertionIndex, newFieldDeclaration));
                return Task.FromResult(Tuple.Create(document.Document.WithSyntaxRoot(newRoot), destination, insertionIndex));
            }
        }
开发者ID:CAPCHIK,项目名称:roslyn,代码行数:61,代码来源:CSharpIntroduceVariableService_IntroduceField.cs

示例2: IntroduceLocalForSingleOccurrenceIntoBlock

        private Document IntroduceLocalForSingleOccurrenceIntoBlock(
            SemanticDocument document,
            ExpressionSyntax expression,
            NameSyntax localName,
            LocalDeclarationStatementSyntax localDeclaration,
            bool allOccurrences,
            CancellationToken cancellationToken)
        {
            var oldStatement = expression.GetAncestorOrThis<StatementSyntax>();
            var newStatement = Rewrite(
                document, expression, localName, document, oldStatement, allOccurrences, cancellationToken);

            if (oldStatement.IsParentKind(SyntaxKind.Block))
            {
                var oldBlock = oldStatement.Parent as BlockSyntax;
                var statementIndex = oldBlock.Statements.IndexOf(oldStatement);

                var newBlock = oldBlock.WithStatements(CreateNewStatementList(
                    oldBlock.Statements, localDeclaration, newStatement, statementIndex));

                var newRoot = document.Root.ReplaceNode(oldBlock, newBlock);
                return document.Document.WithSyntaxRoot(newRoot);
            }
            else if (oldStatement.IsParentKind(SyntaxKind.SwitchSection))
            {
                var oldSwitchSection = oldStatement.Parent as SwitchSectionSyntax;
                var statementIndex = oldSwitchSection.Statements.IndexOf(oldStatement);

                var newSwitchSection = oldSwitchSection.WithStatements(CreateNewStatementList(
                    oldSwitchSection.Statements, localDeclaration, newStatement, statementIndex));

                var newRoot = document.Root.ReplaceNode(oldSwitchSection, newSwitchSection);
                return document.Document.WithSyntaxRoot(newRoot);
            }
            else
            {
                // we need to introduce a block to put the original statement, along with
                // the statement we're generating
                var newBlock = SyntaxFactory.Block(localDeclaration, newStatement).WithAdditionalAnnotations(Formatter.Annotation);

                var newRoot = document.Root.ReplaceNode(oldStatement, newBlock);
                return document.Document.WithSyntaxRoot(newRoot);
            }
        }
开发者ID:XieShuquan,项目名称:roslyn,代码行数:44,代码来源:CSharpIntroduceVariableService_IntroduceLocal.cs

示例3: GetContainerToGenerateInto

        private SyntaxNode GetContainerToGenerateInto(
            SemanticDocument document, ExpressionSyntax expression, CancellationToken cancellationToken)
        {
            var anonymousMethodParameters = GetAnonymousMethodParameters(document, expression, cancellationToken);
            var lambdas = anonymousMethodParameters.SelectMany(p => p.ContainingSymbol.DeclaringSyntaxReferences.Select(r => r.GetSyntax(cancellationToken)).AsEnumerable())
                                                   .Where(n => n is ParenthesizedLambdaExpressionSyntax || n is SimpleLambdaExpressionSyntax)
                                                   .ToSet();

            var parentLambda = GetParentLambda(expression, lambdas);

            if (parentLambda != null)
            {
                return parentLambda;
            }
            else if (IsInExpressionBodiedMember(expression))
            {
                return expression.GetAncestorOrThis<ArrowExpressionClauseSyntax>();
            }
            else
            {
                return expression.GetAncestorsOrThis<BlockSyntax>().LastOrDefault();
            }
        }
开发者ID:XieShuquan,项目名称:roslyn,代码行数:23,代码来源:CSharpIntroduceVariableService_IntroduceLocal.cs

示例4: RewriteExpressionBodiedMemberAndIntroduceLocalDeclaration

		private Document RewriteExpressionBodiedMemberAndIntroduceLocalDeclaration(
			SemanticDocument document,
			ExpressionSyntax expression,
			NameSyntax newLocalName,
			LocalDeclarationStatementSyntax declarationStatement,
			bool allOccurrences,
			CancellationToken cancellationToken)
		{
			var oldBody = expression.GetAncestorOrThis<ArrowExpressionClauseSyntax>();
			var oldParentingNode = oldBody.Parent;
			var leadingTrivia = oldBody.GetLeadingTrivia()
			                           .AddRange(oldBody.ArrowToken.TrailingTrivia);

			var newStatement = Rewrite(document, expression, newLocalName, document, oldBody.Expression, allOccurrences, cancellationToken);
			var newBody = SyntaxFactory.Block(declarationStatement, SyntaxFactory.ReturnStatement(newStatement))
			                           .WithLeadingTrivia(leadingTrivia)
			                           .WithTrailingTrivia(oldBody.GetTrailingTrivia())
			                           .WithAdditionalAnnotations(Formatter.Annotation);

			SyntaxNode newParentingNode = null;
			if (oldParentingNode is BasePropertyDeclarationSyntax)
			{
				var getAccessor = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration, newBody);
				var accessorList = SyntaxFactory.AccessorList(SyntaxFactory.List(new[] { getAccessor }));

				newParentingNode = ((BasePropertyDeclarationSyntax)oldParentingNode).RemoveNode(oldBody, SyntaxRemoveOptions.KeepNoTrivia);

				if (newParentingNode.IsKind(SyntaxKind.PropertyDeclaration))
				{
					var propertyDeclaration = ((PropertyDeclarationSyntax)newParentingNode);
					newParentingNode = propertyDeclaration
						.WithAccessorList(accessorList)
						.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.None))
						.WithTrailingTrivia(propertyDeclaration.SemicolonToken.TrailingTrivia);
				}
				else if (newParentingNode.IsKind(SyntaxKind.IndexerDeclaration))
				{
					var indexerDeclaration = ((IndexerDeclarationSyntax)newParentingNode);
					newParentingNode = indexerDeclaration
						.WithAccessorList(accessorList)
						.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.None))
						.WithTrailingTrivia(indexerDeclaration.SemicolonToken.TrailingTrivia);
				}
			}
			else if (oldParentingNode is BaseMethodDeclarationSyntax)
			{
				newParentingNode = ((BaseMethodDeclarationSyntax)oldParentingNode)
					.RemoveNode(oldBody, SyntaxRemoveOptions.KeepNoTrivia)
					.WithBody(newBody);

				if (newParentingNode.IsKind(SyntaxKind.MethodDeclaration))
				{
					var methodDeclaration = ((MethodDeclarationSyntax)newParentingNode);
					newParentingNode = methodDeclaration
						.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.None))
						.WithTrailingTrivia(methodDeclaration.SemicolonToken.TrailingTrivia);
				}
				else if (newParentingNode.IsKind(SyntaxKind.OperatorDeclaration))
				{
					var operatorDeclaration = ((OperatorDeclarationSyntax)newParentingNode);
					newParentingNode = operatorDeclaration
						.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.None))
						.WithTrailingTrivia(operatorDeclaration.SemicolonToken.TrailingTrivia);
				}
				else if (newParentingNode.IsKind(SyntaxKind.ConversionOperatorDeclaration))
				{
					var conversionOperatorDeclaration = ((ConversionOperatorDeclarationSyntax)newParentingNode);
					newParentingNode = conversionOperatorDeclaration
						.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.None))
						.WithTrailingTrivia(conversionOperatorDeclaration.SemicolonToken.TrailingTrivia);
				}
			}

			var newRoot = document.Root.ReplaceNode(oldParentingNode, newParentingNode);
			return document.Document.WithSyntaxRoot(newRoot);
		}
开发者ID:pabloescribanoloza,项目名称:monodevelop,代码行数:76,代码来源:CSharpIntroduceVariableService_IntroduceLocal.cs


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