Overwrite method declaration

I repeat methods in a type and must rewrite those that are not static. I would have to insert the parameter either as the first or the last parameter, but I cannot figure out how to create a ParameterSyntax object

This is what I still have

 var methods = from m in r.Members.OfType<MethodDeclarationSyntax>() where !m.Modifiers.Contains(Syntax.Token(SyntaxKind.StaticKeyword)) select new { orgMethodDecl = m, rewrittenDecl = RewriteMethodDeclaration(m,name)}; var rewrittenType = r; foreach(var m in methods){ rewrittenType = rewrittenType.ReplaceNode(m.orgMethodDecl, m.rewrittenDecl); } 

and RewriteMethodDeclaration has a problem

  MethodDeclarationSyntax RewriteMethodDeclaration(MethodDeclarationSyntax method, string name) { var p = Syntax.Parameter(); //type dynamic, name: name var parameters = method.ParameterList.AddParameters(p); return method.WithParameterList(parameters); } 
+4
source share
1 answer

Try the following:

 static MethodDeclarationSyntax RewriteMethodDeclaration(MethodDeclarationSyntax method, string name) { var type = Syntax.ParseTypeName("dynamic"); var identifier = Syntax.Identifier(String.Format(" {0}", name)); var p = Syntax.Parameter( new SyntaxList<AttributeListSyntax>(), new SyntaxTokenList(), type, identifier, null); var parameters = method.ParameterList.AddParameters(p); return method.WithParameterList(parameters); } 
+1
source

Source: https://habr.com/ru/post/1496740/


All Articles