views:

250

answers:

3

http://stackoverflow.com/questions/154109/custom-compiler-warnings and
http://stackoverflow.com/questions/968249/c-create-custom-warning-in-visual-studio-if-certain-method-is-used-in-source-co
haven't helped as they deal with code that is under the author's control.

We are using a 3rd party suite of UI controls (DevExpress) in our software and I want to generate a warning when someone uses MessageBox.Show("blah"); instead of XtraMessageBox.Show("blah");

Is there a way to do that?

Thank you

+1  A: 

No there is no direct way. If you think about it you are looking for a compiler warning for some code that you don't even compile.

If you really want this you could use Reflection methods on YOUR compiled assembly to check if any methods/assemblies you don't want have been called. Cecil has a lot of the functionality you need. You could then make this part of your build process.

Foxfire
Thanks for the link to Cecil. Interesting project.
AndrewJacksonZA
A: 

While there's no way you can do real custom compile-time error in .NET, there's a number of third-party tools (both free and commercial) that can inject their validation logic into the build process, usually after the compilation.

Here are three ways I know of to solve you problem:

  1. Resharper 5.0($) will support custom rules / warnings.
  2. In PostSharp(free) you can define OnMethodBoundary aspect, overwrite its CompileTimeValidate method and emit a [post]compile-time error from it.
  3. NDepend can be integrated with your build process ($) to enforce coding policies like that
zvolkov
+3  A: 

This sort of thing can be addressed relatively easily via a custom rule for FxCop/Visual Studio Code Analysis. If you are using Visual Studio Developer Edition, you will even see the rule failures displayed along-side your compilation warnings and errors in the IDE.

Nicole Calinoiu
Thanks for the information about FXCop Nicole. It seems that this is what I'll be doing.
AndrewJacksonZA