我如何更改默认值 生成方法存根 Visaul Studio 中生成带有主体的方法的行为

throw new NotImplementedException();

代替

throw new Exception("The method or operation is not implemented.");
有帮助吗?

解决方案

取自: 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 efactoring\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>

其他提示

还有一个原因:FxCop 捕获任何抛出“Exception”的实例并对其进行标记,但抛出“NotImplementedException”实例是可以接受的。

我实际上喜欢默认行为,因为它确实有这种区别。NotImplementedException 不是您在编写代码时抛出的临时异常。这意味着“我是认真的,我是 真的 不会实施这件事”。如果您保持代码生成器的原样,那么您很容易在代码中区分“我稍后会回来讨论这个”位和“我决定不这样做”位。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top