本文整理汇总了C#中CSharpSyntaxNode.ReplaceNodes方法的典型用法代码示例。如果您正苦于以下问题:C# CSharpSyntaxNode.ReplaceNodes方法的具体用法?C# CSharpSyntaxNode.ReplaceNodes怎么用?C# CSharpSyntaxNode.ReplaceNodes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CSharpSyntaxNode
的用法示例。
在下文中一共展示了CSharpSyntaxNode.ReplaceNodes方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Replace
public static CSharpSyntaxNode Replace(TypeNameReplacementData[] replacedTypeNameArray, CSharpSyntaxNode syntaxNode)
{
var typeNodes = syntaxNode.DescendantNodes()
.OfType<TypeSyntax>()
.Where(f => replacedTypeNameArray.Any(r => r.OldTypeName == f.ToString()));
return syntaxNode.ReplaceNodes(typeNodes, (n1, n2) => {
var name = n1.ToString();
var newName = replacedTypeNameArray.First(f => f.OldTypeName == name).NewTypeName;
var newType = SyntaxFactory.ParseTypeName(newName);
return newType;
});
}
示例2: AddOptional
public static CSharpSyntaxNode AddOptional(CSharpSyntaxNode syntaxNode)
{
var interfaces = syntaxNode.DescendantNodesAndSelf().Where(f => f is InterfaceDeclarationSyntax);
var properties = interfaces.SelectMany(f => f.DescendantNodes().Where(c => c is PropertyDeclarationSyntax));
var methods = interfaces.SelectMany(f => f.DescendantNodes().Where(c => c is MethodDeclarationSyntax));
return syntaxNode.ReplaceNodes(properties.Concat(methods), (node, node2) =>
{
var property = node as PropertyDeclarationSyntax;
var method = node as MethodDeclarationSyntax;
if (property != null)
{
return property.WithIdentifier(SyntaxFactory.Identifier(property.Identifier.ValueText + "?"));
}
return method.WithIdentifier(SyntaxFactory.Identifier(method.Identifier.ValueText + "?"));
});
}