c# - 使用部分类和设计器文件将 Visual Studio 2003 表单转换为 Visual Studio 2005/2008 表单

标签 c# .net visual-studio

将我的 Visual Studio 2003 项目迁移到 VS2005(或 VS2008)后,我的表单仍将位于单个文件中。

VS2005 和 VS2008 上的新表单是使用部分类创建的,其中编辑器生成的所有代码都保存在 Designer.cs 文件中。

由于 VS2005 表单创建是一种更好的处理表单的方法,我想知道是否有一种方法可以将我所有的旧单文件表单转换为 VS2005 分部类方法。

我已经手工完成了一些,但这非常棘手,可能会导致一些严重的错误。

有什么建议吗? PS:我使用的是 Microsoft Visual C# 2008 Express 版。

最佳答案

这似乎是你想要的。

Converting Visual Studio 2003 WinForms to Visual Studio 2005/2008 partial classes :

NET 2.0 introduced partial classes which enables “.designer” files in Visual Studio 2005 and later. That is, all of the visual designer-generated code (control declarations, the InitializeComponent method, etc) can be kept in a file separate from your regular code. When you open up a .NET 1.x Visual Studio 2003 WinForms project up in Visual Studio 2005/2008 it will upgrade your project to .NET 2.0 just fine, but unfortunately it doesn’t migrate your WinForms classes over to the new “.designer” project structure.

Initially I thought this would be a job for a DXCore plug-in (the free framework upon which CodeRush is built) as it provides plug-ins with an object model of the code which could be used to grab all the right members and move them over into a designer file. Before I looked into this though I checked what the options were for simply implementing it as a Visual Studio Macro. I was fully expecting to have to use a regular expression to grep the code file to perform the task, but was pleasantly surprised to find that the Visual Studio extensibility API in available to macros provides a code model (based on the .NET CodeDom I presume) which you can traverse to inspect and manipulate the underlying code. So, here’s what the resulting “ExtractWinFormsDesignerFile” macro does:

  • Locates the first class in the selected project item (DTE.SelectedItems.Item(1).ProjectItem) by traversing the ProjectItem.FileCodeModel.CodeElements
  • Extracts the InitializeComponent and Dispose methods from the class by traversing CodeClass.Members
  • Extracts all control fields: that is, all fields whose type derives from System.Windows.Forms.Control or System.ComponentModel.Container or whose type name starts with System.Windows.Forms
  • Puts all the extracted code into a new “FormName.Designer.cs” file.

This is currently C# only – it could easily be converted to generated VB.NET code or adapted use the FileCodeModel properly and perhaps create the code in an language-agnostic way when generating the designer file. I took a shortcut in just generating the designer file as a string and writing it directly to a file.

To “install”: download the macro text :

    ' -------------------------------------------------------------------------
    ' Extract WinForms Designer File Visual Studio 2005/2008 Macro
    ' -------------------------------------------------------------------------
    ' Extracts the InitializeComponent() and Dispose() methods and control
    ' field delarations from a .NET 1.x VS 2003 project into a VS 2005/8 
    ' style .NET 2.0 partial class in a *.Designer.cs file. (Currently C# 
    ' only)
    ' 
    ' To use: 
    '  * Copy the methods below into a Visual Studio Macro Module (use 
    '    ALT+F11 to show the Macro editor)
    '  * Select a Windows Form in the Solution Explorer
    '  * Run the macro by showing the Macro Explorer (ALT+F8) and double
    '    clicking the 'ExtractWinFormsDesignerFile' macro.
    '  * You will then be prompted to manually make the Form class partial: 
    '    i.e. change "public class MyForm : Form"
    '          to
    '             "public partial class MyForm : Form"
    '
    ' Duncan Smart, InfoBasis, 2007
    ' -------------------------------------------------------------------------

    Sub ExtractWinFormsDesignerFile()
        Dim item As ProjectItem = DTE.SelectedItems.Item(1).ProjectItem
        Dim fileName As String = item.FileNames(1)
        Dim dir As String = System.IO.Path.GetDirectoryName(fileName)
        Dim bareName As String = System.IO.Path.GetFileNameWithoutExtension(fileName)
        Dim newItemPath As String = dir & "\" & bareName & ".Designer.cs"

        Dim codeClass As CodeClass = findClass(item.FileCodeModel.CodeElements)
        Dim namespaceName As String = codeClass.Namespace.FullName

        On Error Resume Next ' Forgive me :-)
        Dim initComponentText As String = extractMember(codeClass.Members.Item("InitializeComponent"))
        Dim disposeText As String = extractMember(codeClass.Members.Item("Dispose"))
        Dim fieldDecls As String = extractWinFormsFields(codeClass)
        On Error GoTo 0

        System.IO.File.WriteAllText(newItemPath, "" _
          & "using System;" & vbCrLf _
          & "using System.Windows.Forms;" & vbCrLf _
          & "using System.Drawing;" & vbCrLf _
          & "using System.ComponentModel;" & vbCrLf _
          & "using System.Collections;" & vbCrLf _
          & "" & vbCrLf _
          & "namespace " & namespaceName & vbCrLf _
          & "{" & vbCrLf _
          & "   public partial class " & codeClass.Name & vbCrLf _
          & "   {" & vbCrLf _
          & "       #region Windows Form Designer generated code" & vbCrLf _
          & "       " & fieldDecls & vbCrLf _
          & "       " & initComponentText & vbCrLf _
          & "       #endregion" & vbCrLf & vbCrLf _
          & "       " & disposeText & vbCrLf _
          & "   }" & vbCrLf _
          & "}" & vbCrLf _
          )
        Dim newProjItem As ProjectItem = item.ProjectItems.AddFromFile(newItemPath)
        On Error Resume Next
        newProjItem.Open()
        DTE.ExecuteCommand("Edit.FormatDocument")
        On Error GoTo 0

        MsgBox("TODO: change your class from:" + vbCrLf + _
               "  ""public class " + codeClass.FullName + " : Form""" + vbCrLf + _
               "to:" + _
               "  ""public partial class " + codeClass.FullName + " : Form""")
    End Sub

    Function findClass(ByVal items As System.Collections.IEnumerable) As CodeClass
        For Each codeEl As CodeElement In items
            If codeEl.Kind = vsCMElement.vsCMElementClass Then
                Return codeEl
            ElseIf codeEl.Children.Count > 0 Then
                Dim cls As CodeClass = findClass(codeEl.Children)
                If cls IsNot Nothing Then
                    Return findClass(codeEl.Children)
                End If
            End If
        Next
        Return Nothing
    End Function

    Function extractWinFormsFields(ByVal codeClass As CodeClass) As String

        Dim fieldsCode As New System.Text.StringBuilder

        For Each member As CodeElement In codeClass.Members
            If member.Kind = vsCMElement.vsCMElementVariable Then
                Dim field As CodeVariable = member
                If field.Type.TypeKind <> vsCMTypeRef.vsCMTypeRefArray Then
                    Dim fieldType As CodeType = field.Type.CodeType
                    Dim isControl As Boolean = fieldType.Namespace.FullName.StartsWith("System.Windows.Forms") _
                       OrElse fieldType.IsDerivedFrom("System.Windows.Forms.Control") _
                       OrElse fieldType.IsDerivedFrom("System.ComponentModel.Container")
                    If isControl Then
                        fieldsCode.AppendLine(extractMember(field))
                    End If
                End If
            End If
        Next
        Return fieldsCode.ToString()
    End Function

    Function extractMember(ByVal memberElement As CodeElement) As String
        Dim memberStart As EditPoint = memberElement.GetStartPoint().CreateEditPoint()
        Dim memberText As String = String.Empty
        memberText += memberStart.GetText(memberElement.GetEndPoint())
        memberStart.Delete(memberElement.GetEndPoint())
        Return memberText
    End Function

and copy the methods into a Visual Studio Macro Module (use ALT+F11 to show the Macro editor).
To use:

  • Select a Windows Form in the Solution Explorer
  • Run the macro by showing the Macro Explorer (ALT+F8) and double-clicking the ‘ExtractWinFormsDesignerFile’ macro. (Obviously you can hook the macro up to a toolbar button if you like.)
  • You will then be prompted to manually make the Form class partial (another bit I was too lazy to work out how to get the macro to do): i.e. change public class MyForm : Form to public partial class MyForm : Form

关于c# - 使用部分类和设计器文件将 Visual Studio 2003 表单转换为 Visual Studio 2005/2008 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2179526/

相关文章:

c# - 如何结束响应并发回 HTTP 代码 404?

c# - 如何在 C# 中创建自定义类型的数组?

c# - 按多个字段对集合进行排序

c# - WCF 授权策略 : Impersonation Problem

c# - 无法加载文件或程序集 'EntityFramework,版本 = 6.0.0.0

C# 工厂方法与泛型强制转换为接口(interface)

c# - 对第 3 方类的多态扩展

.net - Autofixture 奇怪的错误

visual-studio - Visual Studio 源代码控制集成如何与 Perforce 配合使用?

c++ - 什么可以解释 std::cout 不显示任何内容?