Imports System Imports EnvDTE Imports EnvDTE80 Imports System.Diagnostics Public Module EditorHelper '为一个参数封装一般属性访问器 PublicSub 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 IsNothing) Then Return EndIf Debug.Assert(codeElement.Kind = vsCMElement.vsCMElementVariable) Dim codeVar As CodeVariable = CType(codeElement, CodeVariable) Dim fieldName AsString= codeVar.Name Dim codeClass As CodeClass = CType(codeVar.Parent, CodeClass) AddPropertyToClass(codeClass, fieldName, codeVar.Type) Catch ex As Exception '吃掉异常,不做处理或者提示 MsgBox(ex.Message) End Try End Sub PublicSub 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 AsInteger For i =1To 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) EndIf EndIf Catch ex As Exception '吃掉异常 End Try Next Catch ex As Exception '可能并没有选择有效的类定义,这时会抛出异常,忽略 MsgBox(ex.Message) End Try End Sub '根据成员的名称的类型,在类对象中插入属性 PrivateSub AddPropertyToClass(ByVal codeClass As CodeClass, ByVal fieldName AsString, ByVal fieldType AsObject) '生成属性的名称,规则是首先字母大写。如果变量的开头为“_”,移除 Dim propertyName AsString= fieldName If (propertyName.StartsWith("_")) Then propertyName = propertyName.TrimStart("_"c) EndIf 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) Dim propertyEditPoint As EditPoint = codeProperty.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint() 'Getter Dim getter As CodeFunction = codeProperty.Getter Dim getterEditPoint As EditPoint = getter.StartPoint.CreateEditPoint() getterEditPoint.Delete(getter.EndPoint) getterEditPoint.Insert("get{ return "& fieldName &";}") 'Setter Dim setter As CodeFunction = codeProperty.Setter Dim setterEditPoint As EditPoint = setter.StartPoint.CreateEditPoint() setterEditPoint.Delete(setter.EndPoint) setterEditPoint.Insert("set{ "& fieldName &" = value;}") End Sub End Module
posted on
2007-03-16 10:29scdsun
阅读(163)
评论(0)
收藏举报