• behance
  • gitHub
  • google
  • linkedin
  • twitter
MEF, IoC & Visual Studio Suppress Compiler Warnings

MEF, IoC & Visual Studio Suppress Compiler Warnings

If you’re even slightly like me I’m a bit anal about my build processes and codebase.  I like to switch on “Treat warnings as errors” in my builds.  Yes that can be a pain but like StyleCop it can have it’s advantages as well.  I don’t use StyleCop much as I prefer to use Resharper which can provide a lot of the same benefits without the pain.

Anyway, if you use a lot of dependency injection (IoC) or Managed Extensibility Framework (MEF) kinda trickery then you’ll get lots of 0649 errors like:


[Import]
private IDateService _dateService;

"Field '_dateService' is never assigned to, and will always have its default value null"

Well … NO, it won’t thank you very much.  Anyway, cut to the chase, do this …


#pragma warning disable 0649
[Import]
private IDateService _dateService;
#pragma warning restore 0649

Nice.  This works with any error code BTW.  I’ve seen some people just suggest to make the fields public which frankly outrageous, anyone working on my team that wants to design their code around damn compiler warnings can find another job as far as I’m concerned.

Leave a reply

*