Visual Studio uses macros to generate code for get and set properties

Source: Internet
Author: User
Tags class definition

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module Editorhelper
' Encapsulates a generic property accessor for a parameter
Public Sub Encapsulatefield ()
Dim ProjectItem as ProjectItem = DTE. Activedocument.projectitem
Dim FileCodeModel as FileCodeModel = Projectitem.filecodemodel

' Get what's currently selected
Dim SelectText as TextSelection = DTE. Activedocument.selection
' Get to the position of the current cursor
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
' Eat an exception, do not handle or hint
End Try

End Sub

Public Sub Encapsulateallfields ()
Dim ProjectItem as ProjectItem = DTE. Activedocument.projectitem
Dim FileCodeModel as FileCodeModel = Projectitem.filecodemodel

Try
' Get what's currently selected
Dim SelectText as TextSelection = DTE. Activedocument.selection
' Get to the position of the current cursor
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
' If the attribute is already defined, an exception is thrown
' Handle exceptions here, and even if the new attributes are already defined, you can continue with the following code
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 ' static variable does not need to increase the property
Addpropertytoclass (CodeClass, Codevariable.name, Codevariable.type)
End If
End If
Catch ex as Exception
' Eat the anomaly.
End Try
Next
Catch ex as Exception
' may not have selected a valid class definition, it throws an exception and ignores
End Try
End Sub

' Inserts a property in a class object based on the type of the member's name
Private Sub Addpropertytoclass (ByVal codeclass as CodeClass, ByVal fieldName as String, ByVal FieldType as Object)
' Generate the name of the property, the rule is to capitalize first. If the variable starts with "_", remove the
Dim propertyname as String = FieldName
If (Propertyname.startswith ("_")) Then
PropertyName = Propertyname.trimstart ("_" C)
End If
PropertyName = propertyname.substring (0, 1). ToUpper () & Propertyname.substring (1)

' Create a Property object
'-1 means the code is inserted at the bottom of the class
' Vscmaccess.vscmaccesspublic represented as 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) ' Insert return character
Gettereditpoint.lineup ()
Gettereditpoint.indent (, 4) ' Indent 4 positions
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) ' Insert return character
Settereditpoint.lineup ()
Settereditpoint.indent (, 4) ' Indent 4 positions
Settereditpoint.insert (fieldName & "= value;")
End Sub
End Module

-------------Usage-------------------

Open Visual Studio Selection: Tools-> Macros-> Macro Resource management-> right-click in macro Resource management-> new macro Project-> Name must be a step in "AddFields"-> parentheses or it is necessary to create your own hand or do not need to create manually ( New module name must be it: "Editorhelper"-> Open editorhelper copy txt text into it

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.