Καλώς ορίσατε στο dotNETZone.gr - Σύνδεση | Εγγραφή | Βοήθεια

Macro για τη μετατροπή variable σε property (VS2003)

Εντάξει, εντάξει. Το ξέρω! Το VS2005 τα έχει όλα αυτά! Ωσπου όμως να το χρησιμοποιήσουμε, γιατί να μην έχουμε ορισμένες δυνατότητες refactoring και στο VS2003;

Το βρήκα σε μια έτοιμη mdb του CodeLib (http://dotnet.4all.cc) που δίνεται μαζί με την εφαρμογή:

Thanks to Visual Studio.NET macros, it is quite simple to automate the task of transforming a Public field into a Property with the same name that accesses a private field of the same type.

Press Alt-F11 (or invoke the Macro IDE from the Tools|Macros submenu, select one macro module in the left-most treeview, and double-click on it to bring up the corresponding code module. Now cut and paste the following procedure:


Sub MorphVariableIntoProperty()

DTE.ActiveDocument.Selection.StartOfLine _

(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)

DTE.ActiveDocument.Selection.WordRight(True)

DTE.ActiveDocument.Selection.Delete()

DTE.ActiveDocument.Selection.Text = "Private m_"

DTE.ActiveDocument.Selection.EndOfLine(True)

DTE.ActiveDocument.Selection.Copy()

DTE.ActiveDocument.Selection.EndOfLine()

DTE.ActiveDocument.Selection.NewLine(2)

DTE.ActiveDocument.Selection.Text = "Public Property "

DTE.ActiveDocument.Selection.Paste()

DTE.ActiveDocument.Selection.NewLine()

DTE.ActiveDocument.Selection.LineUp(False, 2)

DTE.ActiveDocument.Selection.StartOfLine _

(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)

DTE.ActiveDocument.Selection.WordRight(False, 2)

DTE.ActiveDocument.Selection.WordRight(True)

DTE.ActiveDocument.Selection.Copy()

DTE.ActiveDocument.Selection.LineDown(False, 2)

DTE.ActiveDocument.Selection.Indent(2)

DTE.ActiveDocument.Selection.Text = "Return m_"

DTE.ActiveDocument.Selection.Paste()

DTE.ActiveDocument.Selection.LineDown(False, 3)

DTE.ActiveDocument.Selection.Indent(2)

DTE.ActiveDocument.Selection.Text = "m_"

DTE.ActiveDocument.Selection.Paste()

DTE.ActiveDocument.Selection.Text = "= Value"

End Sub


 

Next, bring up the Tools-Options dialog box, go to Keyboard page, and assign a shortcut key combination to the Morph§VariableIntoProperty macro. Notice that the list of macros contains complete macro names, that are prefixed by their module name. For example, you can assign the following key combination (two keystrokes): Ctrl-Shift-M, Ctrl-Shift-P. To reduce the odds of clashing with other macros, you can reduce the macro's visibility to the text editor.

Now you're ready. Let's say that you have the following variable declaration:


Dim Length As Single
 

Position the caret anywhere on the DIM statement and press the shortcut assigned to the macro defined above. In a few instants Visual Studio renames the variable into m_Length, makes it private, and adds a Public property named Length:


Private m_Length As Single

Public Property Length() As Single

Get

Return m_Length

End Get

Set(ByVal Value As Single)

m_Length = Value

End Set

End Property


 

Notice that this macro heavily relies on Visual Studio's ability to create a Property procedure template when you press the Enter key on a Property declaration. If this feature is disabled, the macro won't work.

 

 

 

Έχουν δημοσιευτεί Τετάρτη, 17 Αυγούστου 2005 11:09 πμ από το μέλος cap
Δημοσίευση στην κατηγορία:

Ενημέρωση για Σχόλια

Αν θα θέλατε να λαμβάνετε ένα e-mail όταν γίνονται ανανεώσεις στο περιεχόμενο αυτής της δημοσίευσης, παρακαλούμε γίνετε συνδρομητής εδώ

Παραμείνετε ενήμεροι στα τελευταία σχόλια με την χρήση του αγαπημένου σας RSS Aggregator και συνδρομή στη Τροφοδοσία RSS με σχόλια

Σχόλια:

Χωρίς Σχόλια

Ποιά είναι η άποψή σας για την παραπάνω δημοσίευση;

(απαιτούμενο)
απαιτούμενο
(απαιτούμενο)
ÅéóÜãåôå ôïí êùäéêü:
CAPTCHA Image