Is it possible to disallow or 'hide' methods to stop the development team using them?
I work in a team of 20+ developers that includes a recent influx of newer members. Our coding standards state that the .First() extension method for IEnumerable should never be used, 开发者_高级运维and instead that we should always use FirstOrDefault() and check for a null return.
I appreciate that some members may want to get into a discussion about training, enforcing coding standards or unit tests to pick up such transgressions, but in principle is it possible to cause the attempted use to throw a compile-time error?
The best thing that you can do in terms of compile time help is:
Make your OWN extension method called "First" that does nothing but throw an exception.
Mark that method as "OBSOLETE" so that you get a compile-time warning.
I think that's the best you can do.
(some comments mentioned concern about the namespace... you would simply use the same namespace as Microsoft does [System.Linq] so that the compiler sees both).
You can write your own rule for StyleCop and enforce it during build.
My personal opinion is that it is waste of time. Is it really so important to forbid one particular method? There are millions of other cases where NullReferenceException can be thrown, and it is something that simply can't be found compile-time.
You might try the following:
1) Overload the functions/methods you want to disallow. 2) Add a obsolete Attribute to the overloaded function/method. 3) Set your compiler settings to not build on errors or warnings.
Doing this would allow the build to fail and any offending code to be immediately noticeable and then the offenders can be identified and trained.
精彩评论