views:

1334

answers:

8

I am developing a set of classes that implement a common interface. A consumer of my library shall expect each of these classes to implement a certain set of static functions. Is there anyway that I can decorate these class so that the compiler will catch the case where one of the functions is not implemented.

I know it will eventually be caught when building the consuming code. And I also know how to get around this problem using a kind of factory class.

Just curious to know if there is any syntax/attributes out there for requiring static functions on a class.

Ed Removed the word 'interface' to avoid confusion.

+3  A: 

Unfortunately, no, there's nothing like this built into the language.

mquander
+8  A: 

No, there is no language support for this in C#. There are two workarounds that I can think of immediately:

  • use reflection at runtime; crossed fingers and hope...
  • use a singleton / default-instance / similar to implement an interface that declares the methods

(update)

Actually, as long as you have unit-testing, the first option isn't actually as bad as you might think if (like me) you come from a strict "static typing" background. The fact is; it works fine in dynamic languages. And indeed, this is exactly how my generic operators code works - it hopes you have the static operators. At runtime, if you don't, it will laugh at you in a suitably mocking tone... but it can't check at compile-time.

Marc Gravell
I would love it if code laughed at me in a suitable mocking tone. Wait, that would happen all the time. Nevermind. ;-)
Robert C. Barth
A: 

No, there would be no point in this feature. Interfaces are basically a scaled down form of multiple inheritance. They tell the compiler how to set up the virtual function table so that non-static virtual methods can be called properly in descendant classes. Static methods can't be virtual, hence, there's no point in using interfaces for them.

dsimcha
There definitely *is* a point in this - just because the methods wouldn't be called via the interface doesn't mean it's not worth testing that they're there. It's a bit like the new T() constraint which C# exposes - the actual implementation of new T() effectively goes via reflection...
Jon Skeet
I don't want them to be virtual, just force them to exist.
tpower
I am with dsimcha on this. If the purpose of explicitly declared interface is not to separate interface from implementation, then there is not much of "interface" left. If you need that some classes have some methods, than you might invent some other languge element, like HasToImplementMethodsAttrib
Dan
Taken 'interface' out of the question it was just providing a bit of context.
tpower
+4  A: 

No. Basically it sounds like you're after a sort of "static polymorphism". That doesn't exist in C#, although I've suggested a sort of "static interface" notion which could be useful in terms of generics.

One thing you could do is write a simple unit test to verify that all of the types in a particular assembly obey your rules. If other developers will also be implementing the interface, you could put that test code into some common place so that everyone implementing the interface can easily test their own assemblies.

Jon Skeet
+1  A: 

While there is no language support for this, you could use a static analysis tool to enforce it. For example, you could write a custom rule for FxCop that detects an attribute or interface implementation on a class and then checks for the existence of certain static methods.

Jeff Yates
A: 

The approach that gets you closer to what you need is a singleton, as Marc Gravell suggested.

Interfaces, among other things, let you provide some level of abstraction to your classes so you can use a given API regardless of the type that implements it. However, since you DO need to know the type of a static class in order to use it, why would you want to enforce that class to implement a set of functions?

Maybe you could use a custom attribute like [ImplementsXXXInterface] and provide some run time checking to ensure that classes with this attribute actually implement the interface you need?

Trap
Runtime checking is no good because it will already be picked up by the compiler when building the consumer code. Just wondering if I could get the compiler to pick it up earlier that's all.
tpower
A: 

The singleton pattern does not help in all cases. My example is from an actual project of mine. It is not contrived.

I have a class (let's call it "Widget") that inherits from a class in a third-party ORM. If I instantiate a Widget object (therefore creating a row in the db) just to make sure my static methods are declared, I'm making a bigger mess than the one I'm trying to clean up.

If I create this extra object in the data store, I've got to hide it from users, calculations, etc.

I use interfaces in C# to make sure that I implement common features in a set of classes.

Some of the methods that implement these features require instance data to run. I code these methods as instance methods, and use a C# interface to make sure they exist in the class.

Some of these methods do not require instance data, so they are static methods. If I could declare interfaces with static methods, the compiler could check whether or not these methods exist in the class that says it implements the interface.

+1  A: 

This is a great question and one that I've encountered in my projects.

Some people hold that interfaces and abstract classes exist for polymorphism only, not for forcing types to implement certain methods. Personally, I consider polymorphism a primary use case, and forced implementation a secondary. I do use the forced implementation technique fairly often. Typically, it appears in framework code implementing a template pattern. The base/template class encapsulates some complex idea, and subclasses provide numerous variations by implementing the abstract methods. One pragmatic benefit is that the abstract methods provide guidance to other developers implementing the subclasses. Visual Studio even has the ability to stub the methods out for you. This is especially helpful when a maintenance developer needs to add a new subclass months or years later.

The downside is that there is no specific support for some of these template scenarios in C#. Static methods are one. Another one is constructors; ideally, ISerializable should force the developer to implement the protected serialization constructor.

The easiest approach probably is (as suggested earlier) to use an automated test to check that the static method is implemented on the desired types. Another viable idea already mentioned is to implement a static analysis rule.

A third option is to use an Aspect-Oriented Programming framework such as PostSharp. PostSharp supports compile-time validation of aspects. You can write .NET code that reflects over the assembly at compile time, generating arbitrary warnings and errors. Usually, you do this to validate that an aspect usage is appropriate, but I don't see why you couldn't use it for validating template rules as well.

Jeffrey Sharp