Visual Studio利用宏產生Get和Set屬性的代碼

來源:互聯網
上載者:User

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module EditorHelper
    '為一個參數封裝一般屬性訪問器
    Public Sub EncapsulateField()
        Dim projectItem As ProjectItem = DTE.ActiveDocument.ProjectItem
        Dim fileCodeModel As FileCodeModel = projectItem.FileCodeModel

        '得到當前選定的內容
        Dim selectText As TextSelection = DTE.ActiveDocument.Selection
        '擷取到當前游標的位置
        Dim point As TextPoint = selectText.ActivePoint

        Try
            Dim codeElement As CodeElement = fileCodeModel.CodeElementFromPoint(point, vsCMElement.vsCMElementVariable)

            If (codeElement Is Nothing) Then
                Return
            End If

            Debug.Assert(codeElement.Kind = vsCMElement.vsCMElementVariable)

            Dim codeVar As CodeVariable = CType(codeElement, CodeVariable)
            Dim fieldName As String = codeVar.Name
            Dim codeClass As CodeClass = CType(codeVar.Parent, CodeClass)

            AddPropertyToClass(codeClass, fieldName, codeVar.Type)
        Catch ex As Exception
            '吃掉異常,不做處理或者提示
        End Try

    End Sub

    Public Sub EncapsulateAllFields()
        Dim projectItem As ProjectItem = DTE.ActiveDocument.ProjectItem
        Dim fileCodeModel As FileCodeModel = projectItem.FileCodeModel

        Try
            '得到當前選定的內容
            Dim selectText As TextSelection = DTE.ActiveDocument.Selection
            '擷取到當前游標的位置
            Dim point As TextPoint = selectText.ActivePoint

            Dim codeElement As CodeElement = fileCodeModel.CodeElementFromPoint(point, vsCMElement.vsCMElementClass)
            Dim codeClass As CodeClass = CType(codeElement, CodeClass)

            Dim i As Integer
            For i = 1 To codeClass.Members.Count
                '如果屬性已經定義,會拋出異常
                '在這裡處理異常,即使新增的屬性已經定義,也可以繼續處理下面的代碼
                Try
                    Dim element As CodeElement = codeClass.Members.Item(i)
                    If (element.Kind = vsCMElement.vsCMElementVariable) Then
                        Dim codeVariable As CodeVariable = CType(element, CodeVariable)
                        If (Not codeVariable.IsShared) Then     '靜態變數不需要增加屬性
                            AddPropertyToClass(codeClass, codeVariable.Name, codeVariable.Type)
                        End If
                    End If
                Catch ex As Exception
                    '吃掉異常
                End Try
            Next
        Catch ex As Exception
            '可能並沒有選擇有效類定義,這時會拋出異常,忽略
        End Try
    End Sub

    '根據成員的名稱的類型,在類對象中插入屬性
    Private Sub AddPropertyToClass(ByVal codeClass As CodeClass, ByVal fieldName As String, ByVal fieldType As Object)
        '產生屬性的名稱,規則是首先字母大寫。如果變數的開頭為“_”,移除
        Dim propertyName As String = fieldName
        If (propertyName.StartsWith("_")) Then
            propertyName = propertyName.TrimStart("_"c)
        End If
        propertyName = propertyName.Substring(0, 1).ToUpper() & propertyName.Substring(1)

        '建立屬性對象
        '-1表示代碼插入到類的最下方
        'vsCMAccess.vsCMAccessPublic表示為public
        Dim codeProperty As CodeProperty = codeClass.AddProperty(propertyName, propertyName, fieldType, -1, vsCMAccess.vsCMAccessPublic)
        'Getter
        Dim getter As CodeFunction = codeProperty.Getter
        Dim getterPoint As TextPoint = getter.GetStartPoint(vsCMPart.vsCMPartBody)
        Dim getterEditPoint As EditPoint = getterPoint.CreateEditPoint()
        getterEditPoint.Delete(getter.GetEndPoint(vsCMPart.vsCMPartBody))
        getterEditPoint.Insert(vbCrLf)      '插入斷行符號符
        getterEditPoint.LineUp()
        getterEditPoint.Indent(, 4)         '縮排4個位置
        getterEditPoint.Insert("return " & fieldName & ";")
        'Setter
        Dim setter As CodeFunction = codeProperty.Setter
        Dim setterPoint As TextPoint = setter.GetStartPoint(vsCMPart.vsCMPartBody)
        Dim setterEditPoint As EditPoint = setterPoint.CreateEditPoint()
        setterEditPoint.Insert(vbCrLf)     '插入斷行符號符
        setterEditPoint.LineUp()
        setterEditPoint.Indent(, 4)         '縮排4個位置
        setterEditPoint.Insert(fieldName & " = value;")
    End Sub
End Module

-------------用法-------------------

開啟Visual Studio 中選擇:工具->宏->宏資源管理->在宏資源管理當中單擊右鍵->建立巨集專案->名稱必須為"AddFields"->括弧當中的這一步或者有必要自己手運建立或者沒必要手動建立(建立模組名稱必須為它:"EditorHelper")->開啟EditorHelper將txt當中的文本複製到它裡面即可    

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.