2011-08-11 179 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[] {}) 
);