zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C#实现为类和函数代码自动添加版权注释信息的方法

c#方法自动代码 实现 函数 信息 添加
2023-06-13 09:15:45 时间

本文实例讲述了C#实现为类和函数代码自动添加版权注释信息的方法,分享给大家供大家参考之用。具体方法如下:

以web项目为例:

一:给类加注释

1.在visualstudio的安装路径下

如:[盘符]:/Programfiles/MicrosoftVisualStudio8/Common7/IDE/ItemTemplates/web/cshare/2052/class.zip,将里面的class.cs改为:

/*----------------------------------------------------------------
//版权所有。
//
//文件名:
//文件功能描述:
//
//
//创建标识:
//
//修改标识:
//修改描述:
//
//修改标识:
//修改描述:
//----------------------------------------------------------------*/
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;

///<summary>
///$safeitemrootname$的摘要说明
///</summary>
publicclass$safeitemrootname$
{
public$safeitemrootname$()
{
//
//TODO:在此处添加构造函数逻辑
//
}
}
/*----------------------------------------------------------------
//版权所有。
//
//文件名:
//文件功能描述:
//
//
//创建标识:
//
//修改标识:
//修改描述:
//
//修改标识:
//修改描述:
//----------------------------------------------------------------*/
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;

///<summary>
///$safeitemrootname$的摘要说明
///</summary>
publicclass$safeitemrootname$
{
public$safeitemrootname$()
{
//
//TODO:在此处添加构造函数逻辑
//
}
}

保存文件即可(先解压,在修改)

二:VS宏脚本添加函数注释模板

现在的IDE越做越强大,为我等懒人省了不少。为了使用将来的代码自己或别人能看懂,注释这种东西必不可少。当为函数添加注释时,格式是固定的。每个函数写一遍,或从别的函数处拷贝过来,即麻烦又容易出错。这种重复劳动让人心烦都有不想写注释的欲望了,这时VS的宏可以干掉这些“脏、乱、累”的体力活。

看了一下,vs2010的宏脚本就是VBScript,很容易上手。我写了一个生成函数注释模板的宏脚本,比较容易,看代码:

ImportsSystem
ImportsEnvDTE
ImportsEnvDTE80
ImportsEnvDTE90
ImportsSystem.Diagnostics

PublicModuleModule1
SubAddFunComment()
DimDocSelAsEnvDTE.TextSelection
DocSel=DTE.ActiveDocument.Selection
DocSel.NewLine()
DocSel.Text="/*******************************************************************"
DocSel.NewLine()
DocSel.Text="*函数名称:"
DocSel.NewLine()
DocSel.Text="*功能:"
DocSel.NewLine()
DocSel.Text="*参数:"
DocSel.NewLine()
DocSel.Text="*返回值:"
DocSel.NewLine()
DocSel.Text="*作者:Lonkil"
DocSel.NewLine()
DocSel.Text="*电子邮箱:lonkil{AT}gmail.com({AT}->@)"
DocSel.NewLine()
DocSel.Text="*创建日期:"+System.DateTime.Now.ToLongDateString()
DocSel.NewLine()
DocSel.Text="*******************************************************************/"
EndSub
EndModule
ImportsSystem
ImportsEnvDTE
ImportsEnvDTE80
ImportsEnvDTE90
ImportsSystem.Diagnostics

PublicModuleModule1
SubAddFunComment()
DimDocSelAsEnvDTE.TextSelection
DocSel=DTE.ActiveDocument.Selection
DocSel.NewLine()
DocSel.Text="/*******************************************************************"
DocSel.NewLine()
DocSel.Text="*函数名称:"
DocSel.NewLine()
DocSel.Text="*功能:"
DocSel.NewLine()
DocSel.Text="*参数:"
DocSel.NewLine()
DocSel.Text="*返回值:"
DocSel.NewLine()
DocSel.Text="*作者:Lonkil"
DocSel.NewLine()
DocSel.Text="*电子邮箱:lonkil{AT}gmail.com({AT}->@)"
DocSel.NewLine()
DocSel.Text="*创建日期:"+System.DateTime.Now.ToLongDateString()
DocSel.NewLine()
DocSel.Text="*******************************************************************/"
EndSub
EndModule

具体的创建步骤:vs2010IDE->工具->宏->新建宏项目,选择要保存的位置。然后将要上面的脚本复制进去,保存即可。

具体的使用:为你编写的宏绑定快捷键,vs2010IDE->工具->选项->在左边列表中选择“键盘”->在右边的“显示命令包含”中,选择你创建宏->将光标定位到”按快捷键”处->输入你想命名的快捷键,比如”Alt+C”,保存即可。

有一点需要注意:VisualStudio2005TeamSuite需要打上SP1补丁,宏方能使用否则无效。

相信本文所述对大家的C#程序设计有一定的借鉴价值。