Visual Studio 2008 addin copies and renames form files, gets duplicate members errors despite rename
I have a VB.NET project in Visual Studio 2008 that I created a specialized addin for. The addin prompts the user to select a database table, takes a template form class's files, copies them to another directory within the project, and renames the form class within the copied files. It then opens the new form and adds controls to it based on the fields in the database table.
The copying of the template form causes the background compiler to give 'duplicate member' errors, such as "Private Sub InitializeComponents(...) has multiple definitions with identical signatures," even though I renamed the files of th开发者_开发问答e new form and the class name within them. Sometimes these errors go away before the new form is opened, but when they stick around the new form doesn't open correctly, and it throws an error instead.
I implemented some code to wait until there are zero errors in the error list before trying to open the new form. This has helped sometimes, but for some reason sometimes the errors don't go away at all until the addin is closed.
I was hoping someone could give tips on how to copy the template form and rename the copy without the background compiler ever noticing duplicate members. If that's not possible, then perhaps someone has an alternative?
Here is my code that copies and renames the template form:
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
After all my attempts at correcting this via Visual Studio's Extensibility API, I gave up with that approach and now do all the dirty work behind the scenes. I take the template form's files, copy them to the new directory, and change their file names and contents all behind the scenes via FileInfo objects and whatnot before I add the new files to the project. This way, VS is never in a state where there are 'duplicate member' errors.
If anyone ever wants to see the code, holler and I'll post it up.
精彩评论