需要初始化对象的新实例

本文关键字:实例 新实例 初始化 对象 | 更新日期: 2023-09-27 18:05:22

以下代码:

        CodeVariableDeclarationStatement variableDeclaration = new CodeVariableDeclarationStatement(
            // Type of the variable to declare.
            typeof(string),
            // Name of the variable to declare.
            "TestString");

生成以下VB。网声明:

Dim TestString As String

我需要做什么改变才能使它看起来像这样:

Dim TestString As New StringBuilder()

我对如何让新关键字出现很感兴趣。

需要初始化对象的新实例

添加CodeObjectCreateExpression作为构造函数的第三个参数:

CodeVariableDeclarationStatement variableDeclaration = new CodeVariableDeclarationStatement(
    // Type of the variable to declare.
    typeof(System.Text.StringBuilder),
    // Name of the variable to declare.
    "TestString",
    // A CodeExpression that indicates the initialization expression for the variable.
    new CodeObjectCreateExpression( typeof(System.Text.StringBuilder), new CodeExpression[] {} )
);