本文整理汇总了C#中ObjectCreationExpressionSyntax.FirstAncestorOfType方法的典型用法代码示例。如果您正苦于以下问题:C# ObjectCreationExpressionSyntax.FirstAncestorOfType方法的具体用法?C# ObjectCreationExpressionSyntax.FirstAncestorOfType怎么用?C# ObjectCreationExpressionSyntax.FirstAncestorOfType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObjectCreationExpressionSyntax
的用法示例。
在下文中一共展示了ObjectCreationExpressionSyntax.FirstAncestorOfType方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateUsing
public static SyntaxNode CreateUsing(SyntaxNode root, ObjectCreationExpressionSyntax objectCreation, SemanticModel semanticModel)
{
SyntaxNode newRoot;
if (objectCreation.Parent.IsKind(SyntaxKind.SimpleAssignmentExpression))
{
var assignmentExpression = (AssignmentExpressionSyntax)objectCreation.Parent;
var statement = assignmentExpression.Parent as ExpressionStatementSyntax;
var identitySymbol = (ILocalSymbol)semanticModel.GetSymbolInfo(assignmentExpression.Left).Symbol;
newRoot = UsedOutsideParentBlock(semanticModel, statement, identitySymbol)
? CreateRootAddingDisposeToEndOfMethod(root, statement, identitySymbol)
: CreateRootWithUsing(root, statement, u => u.WithExpression(assignmentExpression));
}
else if (objectCreation.Parent.IsKind(SyntaxKind.EqualsValueClause) && objectCreation.Parent.Parent.IsKind(SyntaxKind.VariableDeclarator))
{
var variableDeclarator = (VariableDeclaratorSyntax)objectCreation.Parent.Parent;
var variableDeclaration = (VariableDeclarationSyntax)variableDeclarator.Parent;
var statement = (LocalDeclarationStatementSyntax)variableDeclaration.Parent;
newRoot = CreateRootWithUsing(root, statement, u => u.WithDeclaration(variableDeclaration.WithoutLeadingTrivia()));
}
else if (objectCreation.Parent.IsKind(SyntaxKind.Argument))
{
var identifierName = GetIdentifierName(objectCreation, semanticModel);
var variableDeclaration = SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName(@"var"))
.WithVariables(SyntaxFactory.SingletonSeparatedList(SyntaxFactory.VariableDeclarator(SyntaxFactory.Identifier(identifierName))
.WithInitializer(SyntaxFactory.EqualsValueClause(SyntaxFactory.Token(SyntaxKind.EqualsToken), objectCreation))));
var arg = objectCreation.Parent as ArgumentSyntax;
var args = objectCreation.Parent.Parent as ArgumentListSyntax;
var newArgs = args.ReplaceNode(arg, arg.WithExpression(SyntaxFactory.IdentifierName(identifierName)));
StatementSyntax statement = objectCreation.FirstAncestorOfType<ExpressionStatementSyntax>();
if (statement != null)
{
var exprStatement = statement.ReplaceNode(args, newArgs);
var newUsingStatment = CreateUsingStatement(exprStatement, SyntaxFactory.Block(exprStatement))
.WithDeclaration(variableDeclaration);
return root.ReplaceNode(statement, newUsingStatment);
}
statement = (StatementSyntax)objectCreation.Ancestors().First(node => node is StatementSyntax);
var newStatement = statement.ReplaceNode(args, newArgs);
var statementsForUsing = new[] { newStatement }.Concat(GetChildStatementsAfter(statement));
var usingBlock = SyntaxFactory.Block(statementsForUsing);
var usingStatement = CreateUsingStatement(newStatement, usingBlock)
.WithDeclaration(variableDeclaration);
var statementsToReplace = new List<StatementSyntax> { statement };
statementsToReplace.AddRange(statementsForUsing.Skip(1));
newRoot = root.ReplaceNodes(statementsToReplace, (node, _) => node.Equals(statement) ? usingStatement : null);
}
else
{
newRoot = CreateRootWithUsing(root, (ExpressionStatementSyntax)objectCreation.Parent, u => u.WithExpression(objectCreation));
}
return newRoot;
}
示例2: CreateUsing
public static SyntaxNode CreateUsing(SyntaxNode root, ObjectCreationExpressionSyntax objectCreation, SemanticModel semanticModel)
{
SyntaxNode newRoot;
if (objectCreation.Parent.IsKind(SyntaxKind.SimpleAssignmentExpression))
{
var assignmentExpression = (AssignmentExpressionSyntax)objectCreation.Parent;
var statement = assignmentExpression.Parent as ExpressionStatementSyntax;
var identitySymbol = (ILocalSymbol)semanticModel.GetSymbolInfo(assignmentExpression.Left).Symbol;
newRoot = UsedOutsideParentBlock(semanticModel, statement, identitySymbol)
? CreateRootAddingDisposeToEndOfMethod(root, statement, identitySymbol)
: CreateRootWithUsing(root, statement, u => u.WithExpression(assignmentExpression));
}
else if (objectCreation.Parent.IsKind(SyntaxKind.EqualsValueClause) && objectCreation.Parent.Parent.IsKind(SyntaxKind.VariableDeclarator))
{
var variableDeclarator = (VariableDeclaratorSyntax)objectCreation.Parent.Parent;
var variableDeclaration = (VariableDeclarationSyntax)variableDeclarator.Parent;
var statement = (LocalDeclarationStatementSyntax)variableDeclaration.Parent;
newRoot = CreateRootWithUsing(root, statement, u => u.WithDeclaration(variableDeclaration.WithoutLeadingTrivia()));
}
else if (objectCreation.Parent.IsKind(SyntaxKind.Argument))
{
var identifierName = GetIdentifierName(objectCreation, semanticModel);
var variableDeclaration = SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName(@"var"))
.WithVariables(SyntaxFactory.SingletonSeparatedList(SyntaxFactory.VariableDeclarator(SyntaxFactory.Identifier(identifierName))
.WithInitializer(SyntaxFactory.EqualsValueClause(SyntaxFactory.Token(SyntaxKind.EqualsToken), objectCreation))));
var arg = objectCreation.Parent as ArgumentSyntax;
var args = objectCreation.Parent.Parent as ArgumentListSyntax;
var newArgs = args.ReplaceNode(arg, arg.WithExpression(SyntaxFactory.IdentifierName(identifierName)));
StatementSyntax statement = objectCreation.FirstAncestorOfType<ExpressionStatementSyntax>();
if (statement != null)
{
var exprStatement = statement.ReplaceNode(args, newArgs);
var newUsingStatment = CreateUsingStatement(exprStatement, SyntaxFactory.Block(exprStatement))
.WithDeclaration(variableDeclaration);
return root.ReplaceNode(statement, newUsingStatment);
}
statement = (StatementSyntax)objectCreation.Ancestors().First(node => node is StatementSyntax);
var newStatement = statement.ReplaceNode(args, newArgs);
var statementsForUsing = new[] { newStatement }.Concat(GetChildStatementsAfter(statement));
var usingBlock = SyntaxFactory.Block(statementsForUsing);
var usingStatement = CreateUsingStatement(newStatement, usingBlock)
.WithDeclaration(variableDeclaration);
var statementsToReplace = new List<StatementSyntax> { statement };
statementsToReplace.AddRange(statementsForUsing.Skip(1));
newRoot = root.ReplaceNodes(statementsToReplace, (node, _) => node.Equals(statement) ? usingStatement : null);
}
else if (objectCreation.Parent.IsKind(SyntaxKind.SimpleMemberAccessExpression))
{
var newVariableName = objectCreation.Type.ToString();
var newVariableNameParts = newVariableName.Split('.');
newVariableName = newVariableNameParts[newVariableNameParts.Length - 1].ToLowerCaseFirstLetter();
var parentStatement = objectCreation.Parent.FirstAncestorOrSelfThatIsAStatement();
var originalName = newVariableName;
for (int nameIncrement = 1; ; nameIncrement++)
{
var speculativeSymbol = semanticModel.GetSpeculativeSymbolInfo(parentStatement.GetLocation().SourceSpan.Start, SyntaxFactory.IdentifierName(newVariableName), SpeculativeBindingOption.BindAsExpression);
if (speculativeSymbol.Symbol == null) break;
newVariableName = originalName + nameIncrement;
}
var newVariable = SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(SyntaxFactory.ParseTypeName("var"),
SyntaxFactory.SeparatedList(new[] {
SyntaxFactory.VariableDeclarator(newVariableName).WithInitializer(SyntaxFactory.EqualsValueClause(objectCreation))
})));
newRoot = root.TrackNodes(parentStatement, objectCreation);
newRoot = newRoot.ReplaceNode(newRoot.GetCurrentNode(objectCreation), SyntaxFactory.IdentifierName(newVariableName));
var newTrackedParentStatement = newRoot.GetCurrentNode(parentStatement);
newRoot = newRoot.InsertNodesBefore(newTrackedParentStatement, new[] { newVariable });
var statement = (LocalDeclarationStatementSyntax)newRoot.GetCurrentNode(parentStatement).GetPreviousStatement();
var variableDeclaration = statement.Declaration;
var variableDeclarator = variableDeclaration.Variables.First();
newRoot = CreateRootWithUsing(newRoot, statement, u => u.WithDeclaration(variableDeclaration.WithoutLeadingTrivia()));
}
else
{
newRoot = CreateRootWithUsing(root, (ExpressionStatementSyntax)objectCreation.Parent, u => u.WithExpression(objectCreation));
}
return newRoot;
}