分享

VS中为类,函数代码自动添加版权注释信息 - CookBlack - 博客园

 levenstein 2011-04-25

VS中为类,函数代码自动添加版权注释信息

以web项目为例:
一:给类加注释
1.在visual studio 的安装路径下
        如:[盘符]:\Program files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\web\cshare\2052\class.zip ,将里面的class.cs改为:
01 /*----------------------------------------------------------------
02 // 版权所有。 
03 //
04 // 文件名:
05 // 文件功能描述:
06 //
07 // 
08 // 创建标识:
09 //
10 // 修改标识:
11 // 修改描述:
12 //
13 // 修改标识:
14 // 修改描述:
15 //----------------------------------------------------------------*/
16 using System;  
17 using System.Data;  
18 using System.Configuration;  
19 using System.Web;  
20 using System.Web.Security;  
21 using System.Web.UI;  
22 using System.Web.UI.WebControls;  
23 using System.Web.UI.WebControls.WebParts;  
24 using System.Web.UI.HtmlControls;  
25   
26 /// <summary>
27 /// $safeitemrootname$ 的摘要说明
28 /// </summary>
29 public class $safeitemrootname$  
30 {  
31 public $safeitemrootname$()  
32     {  
33 //
34 // TODO: 在此处添加构造函数逻辑
35 //
36     }  
37 }
保存文件即可(先解压,在修改)
其他的也是,找到相应的进行修改就可以。我个人还是偏向第二种。去到哪里都成。
二:VS宏脚本添加函数注释模板
现在的IDE越做越强大,为我等懒人省了不少。为了使用将来的代码自己或别人能看懂,注释这种东西必不可少。当为函数添加注释时,格式是固定的。每个函数写一遍,或从别的函数处拷贝过来,即麻烦又容易出错。这种重复劳动让人心烦都有不想写注释的欲望了,这时VS的宏可以干掉这些“脏、乱、累”的体力活。
看了一下,VS2010的宏脚本就是VBScript,很容易上手。我写了一个生成函数注释模板的宏脚本,比较容易,看代码:
01 Imports System
02 Imports EnvDTE
03 Imports EnvDTE80
04 Imports EnvDTE90
05 Imports EnvDTE90a
06 Imports EnvDTE100
07 Imports System.Diagnostics
08   
09 Public Module ModuleTop
10     Sub AddFunComment()
11         Dim DocSel As EnvDTE.TextSelection
12         DocSel = DTE.ActiveDocument.Selection
13         DocSel.NewLine()
14         DocSel.Text = "/*******************************************************************"
15         DocSel.NewLine()
16         DocSel.Text = "* 版权所有: "
17         DocSel.NewLine()
18         DocSel.Text = "* 类 名 称: "
19         DocSel.NewLine()
20         DocSel.Text = "* 功    能: "
21         DocSel.NewLine()
22         DocSel.Text = "* 参    数: "
23         DocSel.NewLine()
24         DocSel.Text = "* 返 回 值: "
25         DocSel.NewLine()
26         DocSel.Text = "* 作    者:XXXXX"
27         DocSel.NewLine()
28         DocSel.Text = "* 电子邮箱:XXXXXX@gmail.com"
29         DocSel.NewLine()
30         DocSel.Text = "* 创建日期: " + System.DateTime.Now.ToString()
31         DocSel.NewLine()
32         DocSel.Text = "*******************************************************************/"
33     End Sub
34   
35 End Module
具体的创建步骤:VS2010 IDE -> 工具 -> 宏 -> 新建宏项目,选择要保存的位置。然后将要上面的脚本复制进去,保存即可。
image
具体的使用:为你编写的宏绑定快捷键,VS2005 IDE -> 工具 -> 选项 -> 在左边列表中选择“键盘” -> 在右边的“显示命令包含”中,选择你创建宏-> 将光标定位到”按快捷键”处 -> 输入你想命名的快捷键,比如”Alt+C”,保存即可。
image
下面在附上一段,类内部的注释
01 Imports System
02 Imports EnvDTE
03 Imports EnvDTE80
04 Imports EnvDTE90
05 Imports EnvDTE90a
06 Imports EnvDTE100
07 Imports System.Diagnostics
08   
09 Public Module ModuleContent
10     Sub AddFunComment()
11         Dim DocSel As EnvDTE.TextSelection
12         DocSel = DTE.ActiveDocument.Selection
13         DocSel.NewLine()
14         DocSel.NewLine()
15         DocSel.Text = " #region<构造方法和析构方法>"
16         DocSel.NewLine()
17         DocSel.NewLine()
18         DocSel.Text = "#endregion<构造方法和析构方法>"
19         DocSel.NewLine()
20         DocSel.NewLine()
21         DocSel.NewLine()
22         DocSel.Text = "#region<常量>"
23         DocSel.NewLine()
24         DocSel.NewLine()
25         DocSel.Text = "#endregion<常量>"
26         DocSel.NewLine()
27         DocSel.NewLine()
28         DocSel.NewLine()
29         DocSel.Text = "#region<变量>"
30         DocSel.NewLine()
31         DocSel.NewLine()
32         DocSel.Text = "#endregion<变量>"
33         DocSel.NewLine()
34         DocSel.NewLine()
35         DocSel.NewLine()
36         DocSel.Text = "#region<属性>"
37         DocSel.NewLine()
38         DocSel.NewLine()
39         DocSel.Text = "#endregion<属性>"
40         DocSel.NewLine()
41         DocSel.NewLine()
42         DocSel.NewLine()
43         DocSel.Text = "#region<方法>"
44         DocSel.NewLine()
45         DocSel.NewLine()
46         DocSel.Text = "#endregion<方法>"
47         DocSel.NewLine()
48         DocSel.NewLine()
49         DocSel.NewLine()
50         DocSel.Text = "#region<事件>"
51         DocSel.NewLine()
52         DocSel.NewLine()
53         DocSel.Text = "#endregion<事件>"
54     End Sub
55   
56 End Module
实现的效果是
01 #region<构造方法和析构方法>
02  
03 #endregion<构造方法和析构方法>
04  
05  
06 #region<常量>
07  
08 #endregion<常量>
09  
10  
11 #region<变量>
12  
13 #endregion<变量>
14  
15  
16 #region<属性>
17  
18 #endregion<属性>
19  
20  
21 #region<方法>
22  
23 #endregion<方法>
24  
25  
26 #region<事件>
27  
28 #endregion<事件>
快捷键自定义。只要跟现有的冲突就成。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多