使用 AddIn 为 Visual Studio .NET 2003 添加 Try/Catch 块
此 Add-in 通过单击自动在您的方法中添加 try/catch 块。
引言
这是 VS.NET 2003 的一个扩展。此 Add-in 在您的方法中添加 try
/catch
块。这将节省开发时间。它基本上会检查它是否是一个方法,以及是否已经存在 try
/catch
块。如果不存在,它会在您的方法中添加一个 try
/catch
。通常程序员在开发时会忘记编写 try
/catch
,并在最后一刻在所有页面中添加它。那时这将非常有用。他们只需执行此 Add-in,它将自动在您的方法中添加 try
/catch
块。
安装步骤
- 从 AddTryCatchInstall.zip 运行 Setup.exe。
- 选择要安装设置的安装文件夹。
- 单击“关闭”以完成安装。
使用工具
安装 Add-in 后,它将自动列在“工具”菜单中。要使用它,您必须将光标放在方法内并单击此 Add-in。添加到工具栏:要将其添加到工具栏,请转到“工具” -> “自定义” -> 选择“命令”选项卡,然后选择“AddIn”,将 AddTryCatch 拖到工具栏并放下它。
源代码概述
您必须声明此 struct
,其中包含插入符位置、函数名称、函数的起始和结束位置。
private struct SFunction
{
public string FuncName;
public int StPtr;
public int EndPt;
public int CaretPos;
public bool SearchDone;
}
private SFunction OneFunction;
此方法检查活动文档并调用 GetWholeProcedure()
以添加 try
/catch
块。在 Exec
方法中添加 AddingTryCatch()
方法调用 (例如) handler = AddingTryCatch()
;
public bool AddingTryCatch()
{
if (applicationObject.ActiveDocument !=null)
{
GetWholeProcedure();
}
return true;
}
下面列出的代码检查当前窗口中的函数并获取光标所在位置的函数。然后它检查是否已经存在 try
/catch
块。然后它在函数的开头插入一个 try
,并在函数的结尾插入 catch
/finally
。
public void GetWholeProcedure()
{
TextSelection ts = (TextSelection) applicationObject.ActiveDocument.Selection;
EditPoint ep = ts.ActivePoint.CreateEditPoint();
OneFunction.CaretPos=ep.Line;
OneFunction.SearchDone= false;
SearchForFunctionInCurrentWindow();
// if we found the function, get the code
if (OneFunction.SearchDone)
{
ep.MoveToLineAndOffset(OneFunction.StPtr,1);
string s = ep.GetLines(ep.Line,OneFunction.EndPt+1);
// Check for Try/Catch Exists
int found = s.IndexOf("try");
int foundcatch = s.IndexOf("catch(");
int position = 2;
// If not Try/Catch Exists
if(found == -1 || foundcatch == -1 )
{
TextSelection ts1 =
(TextSelection)applicationObject.ActiveDocument.Selection;
// Sets the Insertion point based on webservice or webform file
if(applicationObject.ActiveDocument.Name.IndexOf("asmx.cs")
!= -1 && s.IndexOf("[WebMethod]") != -1 )
position = 3;
else
position = 2;
ts1.GotoLine((OneFunction.StPtr + position),true);
ts1.Copy();
// Inserts Try at start of the function
EditPoint e =ts1.TopPoint.CreateEditPoint();
e.Insert("\t\t\ttry\n\t\t\t{\n");
ts1.Paste();
// Indents the lines
int currentposition = OneFunction.StPtr + position + 1 ;
for(int intLoop = 1 ; intLoop <=(OneFunction.EndPt -
(OneFunction.StPtr+position));intLoop++)
{
ts1.GotoLine((currentposition + intLoop),true);
if(ts1.Text.Equals("") != true )
{
ts1.SelectLine();
ts1.Indent(1);
}
}
// Goes to the end of the function and inserts Catch/Finally Block.
e.LineDown(OneFunction.EndPt-(OneFunction.StPtr+position));
e.Insert("\t\t\t}\n\t\t\tcatch(Exception ex)\n\" +
"t\t\t{\n\t\t\t\tthrow ex;\n\t\t\t}\n\" +
"t\t\tfinally\n\t\t\t{\n\t\t\t}\n");
}
else
{
System.Windows.Forms.MessageBox.Show("Try/Catch Block already exists");
}
}
}
以下函数是找出活动窗口中函数的主要内容。它检查插入符位置的函数,并返回函数的开始和结束点,并将 searchDone
标志设置为 true
。使用的一些扩展类是
ProjectItem
- 表示项目中的一个项目。FileCodeModel
- 允许访问源代码文件中的编程结构。CodeElements
- 表示源代码文件中的一个代码元素或结构。CodeNamespace
- 表示源代码文件中的命名空间结构。CodeClass
- 表示源代码中的一个类。
// enumerates all classes in all namespaces in a window
private void SearchForFunctionInCurrentWindow()
{
ProjectItem pi = (ProjectItem) applicationObject.ActiveWindow.ProjectItem;
FileCodeModel fcm = pi.FileCodeModel;
if ( fcm!=null)
GetFunction(fcm.CodeElements);
}
private void GetFunction(CodeElements elements)
{
// Looks for the function and returns the code.
CodeElements members;
foreach (CodeElement elt in elements)
{
if(OneFunction.SearchDone)
return ;
if (elt.Kind==vsCMElement.vsCMElementFunction )
{
OneFunction.FuncName=elt.Name;
OneFunction.StPtr=elt.StartPoint.Line;
OneFunction.EndPt=elt.EndPoint.Line;
if(OneFunction.CaretPos>= OneFunction.StPtr &&
OneFunction.CaretPos <= OneFunction.EndPt)
{
OneFunction.SearchDone=true;
return;
}
}
else
{
members = GetMembers(elt);
if (members != null )
GetFunction(members);
}
}
}
private CodeElements GetMembers(CodeElement elt)
{
CodeElements members = null ;
if(elt!= null)
{
switch ( elt.Kind)
{
case (vsCMElement.vsCMElementNamespace ):
{
CodeNamespace cdeNS = (CodeNamespace) elt;
members = cdeNS.Members;
break ;
}
case (vsCMElement.vsCMElementClass):
{
CodeClass cdeCL = (CodeClass) elt;
members = cdeCL.Members;
break;
}
case (vsCMElement.vsCMElementFunction):
{
// functions dont have members
members=null;
break;
}
}
}
return members;
}
结论
希望此工具对您有所帮助。祝您有个愉快的一天...