1、Imports SystemImports EnvDTEImports EnvDTE80Imports System.DiagnosticsPublic Module EditorHelper 为一个参数封装一般属性访问器 Public Sub EncapsulateField() Dim projectItem As ProjectItem = DTE.ActiveDocument.ProjectItem Dim fileCodeModel As FileCodeModel = projectItem.FileCodeModel得到当前选定的内容 Dim selectText As Text
2、Selection = 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 = vsCMElemen
3、t.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
4、Sub EncapsulateAllFields() 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如果属性已经定义,会抛出异常在这里处理异常,即使新增的属性已经定义,也可以继续处理下面的代码 Dim eleme
5、nt 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)吃掉异常 Next可能并没有选择有效
6、的类定义,这时会抛出异常,忽略根据成员的名称的类型,在类对象中插入属性 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) p
7、ropertyName = 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 =
8、 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() getterEditPo
9、int.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) sette
10、rEditPoint.LineUp() setterEditPoint.Indent(, 4) setterEditPoint.Insert(fieldName & = value;End Module我定义了两个Public方法:EncapsulateField和EncapsulateAllFields,分别用于为类的一个变量封装属性,或者为类中所有的变量(非静态)封装属性。使用上面的宏的方法很简单,选择“工具”“宏”“宏资源管理器”就可以看到我们已经创建的宏方法,如下图所示:假如你已经编写了这样一段代码:using System;using System.Collections.Gener
11、ic;using System.Text;namespace Demo public class Person private int _id; private string _name; private DateTime _birthDay; 将光标移到“_name”变量上,然后双击“EncapsulateField”宏,就运行了该宏。运行后,你可以得到这样的代码: public string Name get return _name; set _name = value;可以看到“EncapsulateField”宏已经为private string _name;创建了相应的属性。EncapsulateAllFields宏只需要将光标放在Person类的代码区域中,就可以正常执行。例如针对上面的代码,EncapsulateAllFields后可以为Person类中的每一个变量都生成相应的属性。(注:上面的代码中Name属性已经有定义,所有试图再添加Name属性时会抛出异常,在EncapsulateAllFields宏定义中,已经将该异常吃掉,所以,可以正确地为所有变量生成属性)。运行后的代码如下: private DateTime _birth
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1