65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 Visual Studio .NET 宏的样板代码

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.71/5 (5投票s)

2005年4月19日

CPOL

1分钟阅读

viewsIcon

33471

使用 VS.NET 宏插入样板模板。

引言

样板是一种可以反复使用的模板。一些样板包括版权声明、使命宣言、安全警告等。在这里,样板用于描述文件、作者、创建日期、目的等。

我们可以使用 VS.NET 宏将样板模板添加到每个页面。要访问 VS.NET IDE,我们需要引用 DTE 对象。DTE 对象代表 Visual Studio .NET IDE。它位于名为 EnvDTE 的命名空间中。此命名空间的 .NET 程序集名称为 envdte,包含在名为 envdte.dll 的文件中。

创建宏的步骤

  1. 打开 Visual Studio .NET。
  2. 打开宏资源管理器。工具 > 宏 > 宏资源管理器(或按 Alt+F8)。

  3. 右键单击您的 MyMacros 图标,然后选择“新建模块”。

  4. 将新模块命名为 BoilerPlate 并保存它。
  5. 在宏资源管理器中,右键单击新模块,然后选择“新建宏”。

  6. 用“代码”部分中的代码替换所有代码。
  7. 保存宏文件。

代码

'EnvDTE namespace contains the object DTE which represents the VS.NET IDE
Imports EnvDTE
'used to import StringBuilder class
Imports System.Text
Public Module BoilerPlate
    'Method for C# code
    Sub InsertBiolerPlateForCSharp()
        'Get the reference of the current active document
        Dim firstLine As TextSelection = DTE.ActiveDocument.Selection
        Dim boilerPlateText As StringBuilder = New StringBuilder
        boilerPlateText.Append("//********************" & vbNewLine)
        boilerPlateText.Append("// Module Name: " & vbNewLine)
        boilerPlateText.Append("// File Name: " & vbNewLine)
        boilerPlateText.Append("// Author: " & vbNewLine)
        boilerPlateText.Append("// Created Date: " & vbNewLine)
        boilerPlateText.Append("// Description: " & vbNewLine)
        boilerPlateText.Append("// Related pages: " & vbNewLine)
        boilerPlateText.Append("// Displayed: true/false " & vbNewLine)
        boilerPlateText.Append("// Flow when the page is submitted: " & _
                                                               vbNewLine)
        boilerPlateText.Append("// Modification History: " & vbNewLine)
        boilerPlateText.Append("// Author: " & vbNewLine)
        boilerPlateText.Append("// Modified Date: " & vbNewLine)
        boilerPlateText.Append("// Description: " & vbNewLine)
        boilerPlateText.Append("//***********************" & vbNewLine)
        'move to the begining of the file
        firstLine.StartOfDocument()
        'insert the boiler plate
        firstLine.Insert(boilerPlateText.ToString())
        'inserting a new line after the boiler plate
        firstLine.NewLine()
    End Sub
    
    'Method for VB.Net Code
    Sub InsertBiolerPlateForVB()
        Dim firstLine As TextSelection = DTE.ActiveDocument.Selection
        Dim boilerPlateText As StringBuilder = New StringBuilder
        boilerPlateText.Append("'-----------------------" & vbNewLine)
        boilerPlateText.Append("' Module Name: " & vbNewLine)
        boilerPlateText.Append("' File Name: " & vbNewLine)
        boilerPlateText.Append("' Author: " & vbNewLine)
        boilerPlateText.Append("' Created Date: " & vbNewLine)
        boilerPlateText.Append("' Description: " & vbNewLine)
        boilerPlateText.Append("' Related pages: " & vbNewLine)
        boilerPlateText.Append("' Displayed: true/false " & vbNewLine)
        boilerPlateText.Append("' Flow when the page is submitted:" & _
                                                             vbNewLine)
        boilerPlateText.Append("' Modification History: " & vbNewLine)
        boilerPlateText.Append("' Author: " & vbNewLine)
        boilerPlateText.Append("' Modified Date: " & vbNewLine)
        boilerPlateText.Append("' Description: " & vbNewLine)
        boilerPlateText.Append("'-----------------------" & vbNewLine)
        firstLine.StartOfDocument()
        firstLine.Insert(boilerPlateText.ToString())
        firstLine.NewLine()
    End Sub
End Module

如何运行宏

  1. 打开一个已经创建的项目。
  2. 打开一个 C# 文件。
  3. 打开宏资源管理器(如果尚未打开)。

    现在在宏资源管理器中,您可以在 MyMacros 部分下的 BoilerPlate 模块下看到两个函数。

  4. 双击或右键单击并选择“运行”以运行宏。

结论

宏可以简化开发人员的生活。宏是加速开发人员编码工作的一种有用方法。VS.NET 带有强大的录制功能,可以从按键操作录制宏。

© . All rights reserved.