4

我正在迭代一个类型中的方法,并且必须重写那些不是静态的。我必须插入一个参数作为第一个或最后一个参数,但不知道如何创建ParameterSyntax对象

这是我到目前为止所拥有的

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);
                   }

RewriteMethodDeclaration就是我遇到问题的地方

    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

1 回答 1

1

尝试这个:

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);
}
于 2013-08-13T10:17:19.820 回答