2011-07-14 67 views
2

我有一個Visual Studio 2008中的VB.NET項目,我創建了一個專門的插件。插件會提示用戶選擇數據庫表,獲取模板表單類的文件,將它們複製到項目中的另一個目錄中,並在複製的文件中重命名錶單類。然後打開新窗體並根據數據庫表中的字段向其添加控件。Visual Studio 2008插件複製並重命名錶單文件,儘管重命名重複成員錯誤

複製模板表單會導致後臺編譯器給出'重複成員'錯誤,例如「Private Sub InitializeComponents(...)具有多個具有相同簽名的定義」,即使我已將新文件重命名爲形式和他們的類名。有時這些錯誤在新窗體打開之前就消失了,但是當他們堅持新窗體不能正確打開時,它會拋出錯誤。

我實現了一些代碼,在嘗試打開新表單之前,先等待錯誤列表中有零錯誤。這有時會有所幫助,但是由於某種原因,有時候這些錯誤一直沒有消失,直到插件被關閉。

我希望有人可以提供關於如何複製模板表單和重命名副本,而無需後臺編譯器注意到重複成員。如果這不可能,那麼也許有人有另一種選擇?

這裏是我的代碼,拷貝和重命名模板形式:

Private Sub CreateDataForm(ByVal tableName As String, ByVal displayName As String) ', ByVal subDataForms As IList(Of Object)) 
    Try 
     Dim dataFormClassName As String = "frm" & MakeValidName(displayName) 
     Dim dataFormFileName As String = dataFormClassName & cVBSuffix 
     Dim templateFileName As String = DataFormTemplate.Name 
     Dim templateClassName As String = Replace(templateFileName, cVBSuffix, String.Empty) 

     'copy form template to data forms folder 
     'copy files associated with main projectitem 
     Dim newPItem As ProjectItem = Nothing 

     For i As Integer = 1 To DataFormTemplate.FileCount 
      newPItem = DataFormsFolder.ProjectItems.AddFromFileCopy(DataFormTemplate.FileNames(CShort(i))) 
     Next 

     'copy files associated with sub projectitems 
     For Each item As ProjectItem In DataFormTemplate.ProjectItems 
      For i As Integer = 1 To item.FileCount 
       DataFormsFolder.ProjectItems.AddFromFileCopy(item.FileNames(CShort(i))) 
      Next 
     Next 

     newPItem.Name = dataFormFileName 

     newPItem.ContainingProject.Save() 

     'fix class name for dataform template 
     FixDataFormClassName(DataFormTemplate, dataFormClassName, templateClassName) 

     newPItem.ContainingProject.Save() 

     Application.DoEvents() 

     'get table metadata 
     Dim lGetColumnInfo As DataColumnCollection = GetColumnInfo(tableName) 


     'add field controls 
     ConvertColumnInfoToFormControls(newPItem, lGetColumnInfo, tableName, displayName) 

    Catch ex As Exception 
     DisplayExceptionMessage(ex) 
    End Try 
End Sub 

Private Shared Sub FixDataFormClassName(ByVal pItem As ProjectItem, ByVal dataFormClassName As String, ByVal templateClassName As String) 
    If pItem.Document IsNot Nothing Then 
     pItem.Document.Close(vsSaveChanges.vsSaveChangesPrompt) 
    End If 
    For i As Integer = 1 To pItem.FileCount 
     Dim dftFile As New IO.FileInfo(pItem.FileNames(CShort(i))) 
     Dim tr As IO.TextReader = dftFile.OpenText() 'IO.FileMode.Open, IO.FileAccess.ReadWrite, IO.FileShare.None) 
     Dim sb As New Text.StringBuilder 
     Dim newData As String = tr.ReadToEnd().Replace(dataFormClassName, templateClassName) 
     tr.Close() 
     Dim sw As New IO.StreamWriter(dftFile.FullName) 
     sw.Write(newData) 
     sw.Close() 
    Next 

    If pItem.FileCodeModel IsNot Nothing Then CType(pItem.FileCodeModel, FileCodeModel2).Synchronize() 

    For Each item As ProjectItem In pItem.ProjectItems 
     FixDataFormClassName(item, dataFormClassName, templateClassName) 
    Next 
End Sub 

回答

1

我在通過Visual Studio的可擴展API糾正這一切的嘗試後,我放棄了這種做法,現在做後面的所有的髒活場面。我將模板表單的文件複製到新目錄,並通過FileInfo對象在後臺更改文件名和內容,以及在將新文件添加到項目之前進行更改。這樣,VS永遠不會處於有'重複成員'錯誤的狀態。

如果有人想看代碼,我會把它貼出來。

+0

希望我知道我的賞金是不退還的... – Ski