Just RUN

A scientist builds in order to learn; an engineer learns in order to build.

一段生成属性的宏。

执行此宏后会弹出一个Form,在两个TextBox里输入类型和名称,会自动在编辑区中自动添加

        private int typeid;

        
public int Typeid
        
{
            
get {return this.typeid;}
            
set {this.typeid = value;}
        }

这样的property,完整的宏如下,超级郁闷为什么宏不能用C#写

    Sub PropertyGenerator()

        
Dim form As Form

        form 
= New Form

        form.TopMost 
= True

        form.Controls.Add(
New TextBox)
        form.Controls.Add(
New TextBox)

        form.Controls(
0).Text = "type"
        form.Controls(1).Text = "name"
        form.Controls(0).Dock = DockStyle.Bottom
        form.Controls(
1).Dock = DockStyle.Bottom

        form.ShowDialog()

        
Dim type As String
        
Dim name As String
        
Dim capitaledName As String

        type 
= form.Controls(0).Text
        name 
= form.Controls(1).Text
        capitaledName 
= Char.ToUpper(name.Chars(0)) & name.Substring(1)

        form 
= Nothing

        DTE.ActiveDocument.Selection.Text 
= "private " & type & " " & name & ";"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text 
= "public " & type & " " & capitaledName & ""
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text 
= "{"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text 
= "get {return this." & name & ";}"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text 
= "set {this." & name & " = value;}"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text 
= "}"
        DTE.ActiveDocument.Selection.NewLine()

    
End Sub


        Dim line As String

        
With DTE.ActiveDocument.Selection
            line 
= .Text
        
End With

        
Dim parts As String()

        parts 
= line.Split(" ")

        
If Not parts.Length = 3 Then
            
Exit Sub
        
End If

        
Dim type As String
        
Dim name As String
        
Dim capitaledName As String

        type 
= parts(1)
        name 
= parts(2).Replace(";""")
        capitaledName 
= Char.ToUpper(name.Chars(0)) & name.Substring(1)

        DTE.ActiveDocument.Selection.Text 
= "private " & type & " " & name & ";"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text 
= "public " & type & " " & capitaledName & ""
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text 
= "{"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text 
= "get {return this." & name & ";}"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text 
= "set {this." & name & " = value;}"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text 
= "}"
        DTE.ActiveDocument.Selection.NewLine()

posted on 2004-10-10 13:40    阅读(1583)  评论(7编辑  收藏  举报

导航