扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:朱先忠编译 来源:天极网 2007年11月4日
关键字: Visual Studio 代码生成器
Imports EnvDTE Imports EnvDTE80 Imports System.Diagnostics Public Class Refactoring Public Shared Sub EncapsulateField() Dim projectItem As ProjectItem = DTE.ActiveDocument.ProjectItem Dim fileCodeModel As FileCodeModel = projectItem.FileCodeModel ’ 得到当前的选定内容 Dim selection As TextSelection = DTE.ActiveDocument.Selection ’得到当前的光标位置 Dim point As TextPoint = selection.ActivePoint ’尽量读取当前位置作为一个代码元素 Dim codeElement As CodeElement = fileCodeModel.CodeElementFromPoint( _ point, vsCMElement.vsCMElementVariable) If (codeElement Is Nothing) Then MsgBox("Place mouse cursor on field before running this macro.", MsgBoxStyle.Exclamation) Return End If Debug.Assert(codeElement.Kind = vsCMElement.vsCMElementVariable) ’我们测试过了,所以知道这是个变量 Dim codeVariable As CodeVariable = CType(codeElement, CodeVariable) Dim fieldName As String = codeVariable.Name Dim fieldType As String = codeVariable.Type.AsString ’重命名该字段,以使不发生属性冲突问题 codeVariable.Name = "F" & fieldName ’确保字段是private codeVariable.Access = vsCMAccess.vsCMAccessPrivate ’得到变量的parent Dim codeClass As CodeClass = CType(codeVariable.Parent, CodeClass) ’添加一个新属性 Dim codeProperty As CodeProperty = codeClass.AddProperty("dummy", "dummy", fieldType, codeElement) codeProperty.Name = fieldName ’实现getter Dim getter As EditPoint = codeProperty.Getter.GetStartPoint( vsCMPart.vsCMPartBody).CreateEditPoint getter.LineDown() getter.Indent(, 3) getter.Insert("Return " + codeVariable.Name) ’实现setter Dim setter As EditPoint = codeProperty.Setter.GetStartPoint( _ vsCMPart.vsCMPartBody).CreateEditPoint setter.LineDown() setter.Indent(, 3) setter.Insert(codeVariable.Name + " = Value") End Sub End Class |
Public Foo as Integer |
Private FFoo As Integer Public Property Foo As Integer Get Return FFoo End Get Set(ByVal value As Integer) FFoo = value End Set End Property |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者