需要初始化对象的新实例

时间:2011-08-11 15:44:21

标签: c# .net vb.net codedom

以下代码:

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

生成以下VB.Net声明:

Dim TestString As String

我需要做出哪些改变才能看起来像这样:

Dim TestString As New StringBuilder()

我对如何显示新关键字感兴趣。

1 个答案:

答案 0 :(得分:4)

在构造函数中添加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[] {} )
);
相关问题