Custom Rules for Code Analysis: Looking for Sample
In our project we do not allow code to throw exceptions directly; instead we use an Exception Helper Class that handles the logging and instrumentation based on certain settings then rethrows the exception.
We need a rule that verifies that no one is using THROW in their code anywhere. I looked to create a custom rule we could include in our check-in policy but I could not find anything. Does anyone have any pointers?
Best regards, Jerry
Custom rules are officially unsupported in VS2005. The current SDK is currently being reconceived, which means that any investment in rules today will entail further work in the future. We are currently designing a stable representation for metadata and flow control that will be documented in a future version. There are many threads on this topic, in this forum and in the dedicated FxCop forum. Here's a link to an FAQ re: the general state of the SDK. It also contains pointers to several rules samples:
http://www.amazon.com/exec/obidos/tg/detail/-/0321246756/qid=1130854802/sr=8-1/ref=pd_bbs_1/002-2872601-5856864?v=glance&s=books&n=507846
The following post contains some additional code that might be a helpful start:
http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=87015
The rule you're looking for would actually be quite straightforward to write. Your rule would crack the Method.Instructions and fire a violation on any detected OpCode.Throw instruction.
We have done sufficient testing to insure that it is generally possible to load and execute a custom rule in VS2005. In the next version, we plan to have better UI and deployment support, as well as a stable metadata representation, as mentioned previously.