8

如何更改 Visaul Studio 中的默认生成方法存根行为以生成带有正文的方法

throw new NotImplementedException();

代替

throw new Exception("The method or operation is not implemented.");
4

2 回答 2

8

取自:http: //blogs.msdn.com/ansonh/archive/2005/12/08/501763.aspx

Visual Studio 2005 支持面向 1.0 版本的紧凑框架。为了保持紧凑框架的体积小,它不包括桌面框架中存在的所有相同类型。不包括的类型之一是 NotImplementedException。

您可以通过编辑代码片段文件来更改生成的代码: C:\Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033\Refactoring\MethodStub.snippet并将声明部分更改为以下内容:

        <Declarations>
            <Literal Editable="true">
                <ID>signature</ID>
                <Default>signature</Default>
            </Literal>
            <Literal>
                <ID>Exception</ID>
                <Function>SimpleTypeName(global::System.NotImplementedException)</Function>
            </Literal>
        </Declarations>
于 2008-09-05T15:17:12.593 回答
1

还有另一个原因:FxCop 捕获任何抛出“异常”的实例并标记它,但抛出“NotImplementedException”实例是可以接受的。

I actually like the default behavior, because it does have this differentiation. NotImplementedException is not a temporary exception to be thrown while you're working your way through your code. It implies "I mean it, I'm really not going to implement this thing". If you leave the codegen the way it is, it's easy for you to differentiate in the code the "I will come back to this later" bits from "I've decided not to do this" bits.

于 2008-09-05T15:41:07.120 回答