现在的位置: 首页 > 综合 > 正文

Field 包装成property的宏

2011年02月06日 ⁄ 综合 ⁄ 共 1792字 ⁄ 字号 评论关闭
支持单行多行、行尾注释。
VB不熟,垃圾代码很多 :)

    Sub Field2Porperty()
        Dim str As String
        str = DTE.ActiveDocument.Selection.Text()
        Dim lines As String()
        lines = System.Text.RegularExpressions.Regex.Split(str, "\r")
        Dim i As Integer = 0
        Dim r As New System.Text.StringBuilder
        While i < lines.Length
            Dim words As String()
            Dim modifier As String
            Dim type As String
            Dim name As String
            Dim comment As String
            words = System.Text.RegularExpressions.Regex.Split(lines(i), "[;\s]+")
            If words.Length > 1 Then
                Dim shift = IIf(words(0) = "", 1, 0)
                If (words.Length - shift > 2) Then
                    If (Not (words(shift + 2).StartsWith("//") Or words(shift + 2) = "")) Then
                        modifier = words(shift)
                        shift += 1
                    End If
                End If
                If (words.Length - shift > 1) Then
                    type = words(shift + 0)
                    name = words(shift + 1)
                    If (words.Length - shift > 2) Then
                        comment = words(shift + 2)
                    End If
                    If (type <> "" And name <> "") Then
                        'private PointF location;
                        r.Append("#region " & type & " " & name & " " & comment & vbLf)
                        r.Append(IIf(modifier = "", "", modifier) & type & " _" & name & ";" & vbLf)
                        r.Append("public " & type & " " & name.Substring(0, 1).ToUpper() & name.Substring(1) & " {" & vbLf)
                        r.Append("get { return _" & name & "; } " & vbLf)
                        r.Append("set { _" & name & " = value; }" & vbLf)
                        r.Append("}" & vbLf)
                        r.Append("#endregion" & vbLf & vbLf)
                    End If
                End If
                i += 1
            End If
        End While
        DTE.ActiveDocument.Selection.Text = r.ToString()
    End Sub

抱歉!评论已关闭.