Use macro to generate custom exception code in VS.NET 2003

zhaozj2021-02-16  50

When writing the program, every time I have to pay a lot of an exception, I always have to be a big pile of code, and these code is imagined, it is nothing more than the class that is inherited from System.exception, and then Add three constructors and a self-specific message string, in order to simplify this physical labor, I use a small code with the Macro editor comes with the VS2003 to help me complete these things.

1. First open your macro editor (Press ALT F11 in VS2003): Use the following code COPY with editor.

Imports envdteimports system.diagnosticsimports Microsoft.Office.core

Sub Addexception () DIM INPUT AS STRING DIM EXPNAME AS STRING DIM EXPMSG AS STRING

Dim split As String () = Nothing Dim delimStr As String = ",:;." Dim delimiter As Char () = delimStr.ToCharArray () Dim objTextDoc As TextDocument Dim objEditPt_Begin As EditPoint Dim objEditPt_End As EditPoint

INPUT = INPUTBOX ("INPUT AS: EXCEPTIONNAME; Message", "Auto Complete Exception", String.empty

If INPUT.LENGTH = 0 THEN RETURN END IF

Split = INPUT.SPLIT (DELIMITER)

ExpName = split (0)

If split.length = 2 THEN EXPMSG = Split (1) else expmsg = "General Exception of" & expname end

DTE.undocontext.open ("AddException", False)

Objtextdoc = DTE.ActiveDocument.object ("textdocument") Objeditpt_begin = objtextdoc.seection.activePoint.createEditPoint ()

objEditPt_Begin.Insert ( "public class" & expName & ": System.Exception" & vbCrLf) objEditPt_Begin.Insert ( "{" & vbCrLf) objEditPt_Begin.Insert ( "const string errMsg =" "" & expMsg & "" ";" & vbCrLf) objEditPt_Begin.Insert (vbCrLf) objEditPt_Begin.Insert ( "public" & expName & "(): base (errMsg)" & vbCrLf) objEditPt_Begin.Insert ( "{}" & vbCrLf) objEditPt_Begin.Insert (vbCrLf) objEditPt_Begin. Insert ("String AuxMessage): Base (String.Format (" {0} - {1} "", errmsg, auxmessage) "& vbcrlf) Objeditpt_Begin.insert (" {} "& VBCRLF) Objeditpt_Begin.insert (vbcrlf) Objeditpt_Begin.insert ("PUBLIC" & EXPNAME & ": Base (String.Format):, Errror, AuxMessage, inner) "& vbCrLf) objEditPt_Begin.Insert (" {} "& vbCrLf) objEditPt_Begin.Insert ("} "& vbCrLf) objEditPt_End = objTextDoc.Selection.ActivePoint.CreateEditPoint () objEditPt_Begin.LineUp (13)

Objeditpt_begin.smartFormat (Objeditpt_end)

DTE.undocontext.close ()

End Sub

2. Use the right-click menu in MacroExplorer to make this macro.

3. After the macro is running, then a input box will appear, enter in the following format: ExceptionName Splitter ExceptionMSG

For example: MyException; this is my exception

After entering the bus, you will generate the Exception code you want in the file. Hey, feel very cool? You can also assign this macro to this macro in Option-> Keyboard, for example: Alt 1, Try again, the degree of pain of the sensation of the write code is at least 10%. :)

转载请注明原文地址:https://www.9cbs.com/read-20532.html

New Post(0)