Is there *any* way to force compile errors if a method is called with a particular type?

I know this is hacky and typically we restrict callers inclusively through polymorphic base classes, etc. But…

I am working with a code base which has a method that–for reasons that we probably don't need to go into–compiles fine but fails at runtime. I consider this bad behavior. Refactoring all of the "good" types to have a common interface is probably too heavy, so I'm looking for a way to give some compile-time feedback that the method should not be called with particular classes. I could tag those classes with an empty interface or something if that can help with compile-time type checking but as I understand it there are no "exclusive" compile time options for this (ie no "void foo<T>() where T is not blah").

Are there any options for doing this? There is always static analysis but that is usually an extra build step that would be hit well after the frustration from this has been resolved.

by haha_ok via /r/csharp

Leave a Reply