本文整理汇总了C#中ExpressionSyntax.ReplaceNode方法的典型用法代码示例。如果您正苦于以下问题:C# ExpressionSyntax.ReplaceNode方法的具体用法?C# ExpressionSyntax.ReplaceNode怎么用?C# ExpressionSyntax.ReplaceNode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ExpressionSyntax
的用法示例。
在下文中一共展示了ExpressionSyntax.ReplaceNode方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SemanticsChangedAsync
private async Task<bool> SemanticsChangedAsync(
Document document,
VariableDeclarationSyntax declaration,
ExpressionSyntax invocationOrCreation,
TypeSyntax newType,
IdentifierNameSyntax identifier,
DeclarationExpressionSyntax declarationExpression,
CancellationToken cancellationToken)
{
if (newType.IsVar)
{
// Options want us to use 'var' if we can. Make sure we didn't change
// the semantics of teh call by doing this.
// Find the symbol that the existing invocation points to.
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var previousSymbol = semanticModel.GetSymbolInfo(invocationOrCreation).Symbol;
var annotation = new SyntaxAnnotation();
var updatedInvocationOrCreation = invocationOrCreation.ReplaceNode(
identifier, declarationExpression).WithAdditionalAnnotations(annotation);
// Note(cyrusn): "https://github.com/dotnet/roslyn/issues/14384" prevents us from just
// speculatively binding the new expression. So, instead, we fork things and see if
// the new symbol we bind to is equivalent to the previous one.
var newDocument = document.WithSyntaxRoot(
root.ReplaceNode(invocationOrCreation, updatedInvocationOrCreation));
var newRoot = await newDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var newSemanticModel = await newDocument.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
updatedInvocationOrCreation = (ExpressionSyntax)newRoot.GetAnnotatedNodes(annotation).Single();
var updatedSymbol = newSemanticModel.GetSymbolInfo(updatedInvocationOrCreation).Symbol;
if (!SymbolEquivalenceComparer.Instance.Equals(previousSymbol, updatedSymbol))
{
// We're pointing at a new symbol now. Semantic have changed.
return true;
}
}
return false;
}
示例2: RewritePostfixUnarys
protected ExpressionSyntax RewritePostfixUnarys(ExpressionSyntax node)
{
var expressions = node.DescendentNodesAndSelf().OfType<PostfixUnaryExpressionSyntax>().ToArray();
if (expressions.Length == 0)
return node;
List<string> names = new List<string> (expressions.Length);
foreach (var expr in expressions)
{
if (expr.Kind != SyntaxKind.PostIncrementExpression && expr.Kind != SyntaxKind.PostDecrementExpression)
continue; // These aren't the droids we're looking for.
IdentifierNameSyntax name = FindIdentifierName (expr);
if (name == null)
continue; // We didn't find a name
if (names.Contains (name.PlainName))
{
var newExpr = GetLogExpression (name.PlainName, expr);
node = node.ReplaceNode (expr, newExpr);
}
else
names.Add (name.PlainName);
}
// We'll use a string builder construct our expression.
StringBuilder builder = new StringBuilder ("LogPostfixValues ((");
builder.Append (node.ToString());
builder.Append ("), ");
bool found = false;
foreach (string name in names)
{
if (found)
builder.Append (", ");
// We need to specify our generic types so we match
// the params argument type
builder.Append ("new Tuple<string, object> (\"");
builder.Append (name);
builder.Append ("\", ");
builder.Append (name);
builder.Append (")");
found = true;
}
if (!found)
return node; // We didn't find any usable expressions
builder.Append (")");
// Parse and return our new expression.
return Syntax.ParseExpression (builder.ToString());
}