如何更改" Generate Method Stub"以在VS中引发NotImplementedException?

时间:2020-03-05 18:48:16  来源:igfitidea点击:

如何在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 supports targeting the 1.0 version of the compact framework.  In order to keep the size of the compact framework small, it does not include all of the same types that exist in the desktop framework.  One of the types that is not included is 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>

回答

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

我实际上喜欢默认行为,因为它确实具有这种区别。 NotImplementedException不是在处理代码时抛出的临时异常。这意味着"我是说真的,我真的不会实现这个东西"。如果按原样保留代码源,则很容易在代码中区分"我稍后会再来"位与"我决定不这样做"位。