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當中的文本複製到它裡面即可