本文整理汇总了C#中MemberDeclarationSyntax.ReplaceNode方法的典型用法代码示例。如果您正苦于以下问题:C# MemberDeclarationSyntax.ReplaceNode方法的具体用法?C# MemberDeclarationSyntax.ReplaceNode怎么用?C# MemberDeclarationSyntax.ReplaceNode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MemberDeclarationSyntax
的用法示例。
在下文中一共展示了MemberDeclarationSyntax.ReplaceNode方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExtractToExpressionTree
private async Task<Document> ExtractToExpressionTree(Document document, MemberDeclarationSyntax declaration, CancellationToken c)
{
var typeSyntax = declaration.Ancestors().OfType<TypeDeclarationSyntax>().First();
var sm = await document.GetSemanticModelAsync(c);
var type = sm.GetDeclaredSymbol(typeSyntax, c);
var symbol = sm.GetDeclaredSymbol(declaration);
string name = declaration is MethodDeclarationSyntax ?
((MethodDeclarationSyntax)declaration).Identifier.ToString() :
((PropertyDeclarationSyntax)declaration).Identifier.ToString();
string candidateName = name + "Expression";
if (type.MemberNames.Contains(candidateName))
candidateName = Enumerable.Range(2, 1000).Select(i => name + "Expression" + i).First(n => !type.MemberNames.Contains(n));
List<ParameterSyntax> parameterList = GetParameters(declaration, type, symbol);
TypeSyntax returnType = GetReturnType(declaration);
var bodyExpression = GetSingleBody(declaration);
var newField = GetStaticField(candidateName, parameterList, returnType, bodyExpression, sm, type);
var newBody = GetNewBody(candidateName, parameterList);
var newDeclaration = declaration.ReplaceNode(bodyExpression, newBody);
MoveInitialTrivia(ref newField, ref newDeclaration);
var newMembers = typeSyntax.Members.ReplaceRange(declaration, new[] { newField, newDeclaration });
var newType = typeSyntax is StructDeclarationSyntax ?
(TypeDeclarationSyntax)((StructDeclarationSyntax)typeSyntax).WithMembers(newMembers) :
(TypeDeclarationSyntax)((ClassDeclarationSyntax)typeSyntax).WithMembers(newMembers);
var docRoot = await document.GetSyntaxRootAsync();
var newDoc = docRoot.ReplaceNode(typeSyntax, newType);
var usings = newDoc.ChildNodes().OfType<UsingDirectiveSyntax>();
if (usings.Any() && !usings.Any(a => a.Name.ToString() == "System.Linq.Expressions"))
newDoc = newDoc.InsertNodesAfter(usings.LastOrDefault(), new[] {
SyntaxFactory.UsingDirective(
SyntaxFactory.IdentifierName("System").Qualified("Linq").Qualified("Expressions")
).NormalizeWhitespace().WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed)
});
return document.WithSyntaxRoot(newDoc);
}