2011-08-11 180 views
2

的新实例,下面的代码:需要初始化对象

 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() 

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

回答

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[] {}) 
);