SyntaxFactory 生成构造函数并调用基类构造函数

par*_*ix2 6 c# roslyn

我编写了根据初始状态生成新类的代码。Roslyn 为其提供了类SyntaxFactory,但我不明白如何通过调用基类来生成构造函数,如下所示:

public TestClientApi(String entryPoint) : **base(entryPoint)**
{
    _entryPoint = entryPoint;
}
Run Code Online (Sandbox Code Playgroud)

https://github.com/ddydeveloper/Roslyn.ApiClient.Codegen

任何想法?

Jef*_*ado 6

您需要使用初始值设定项创建构造函数声明。

ConstructorDeclaration("TestClientApi")
    .WithInitializer(
        ConstructorInitializer(SyntaxKind.BaseConstructorInitializer)
                // could be BaseConstructorInitializer or ThisConstructorInitializer
            .AddArgumentListArguments(
                Argument(IdentifierName("entryPoint"))
            )
    )
    ...
Run Code Online (Sandbox Code Playgroud)

  • 顺便说一句,这假设您的代码文件中有一个静态 using :“using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;” (2认同)