I've worked with singletons in the past and I'm aware it's a solution for some people who are trying solve the static interface problem. In my case, I can't really use a singleton because I have an external class that I'm inheriting from and I have no control over this one (a library).
Basically, I have many classes inheriting from "TableRow" (the class in the library) and I need every single of these classes to implement a certain static method (ex: GetStaticIdentifier). Eventually, I need to store these objects in one of their base types, and use the static method on this specific type.
My question, is there another solution to this than using a singleton? Is there a feature I'm not aware of in C# that would help me solve this problem?
It seems you want to supply some meta-information along with the subclasses of
TableRow
; meta-information that can be retrieved without instantiating the particular subclass.While .NET lacks static interfaces and static polymorphism, this can (to some extent, see below) be solved with custom attributes. In other words, you can define a custom attribute class that stores the information you want to associate with your type:
You can then apply this custom attribute to your
TableRow
subclasses:You can then retrieve the
Type
instance forMyTableRow
(or any other subclass ofTableRow
) and use theGetCustomAttributes
method to retrieve theStaticIdentifierAttribute instance and read out the value stored in its
StaticIdentifier` property for the class.The drawback compared to static interfaces and static polymorphism is that it is not compile-time ensured that each
TableRow
subclass actually has that attribute; you will have to catch that (and either throw an exception, or ignore the respectiveTableRow
subclass) at runtime.Moreover, you cannot ensure that the attribute is only applied to
TableRow
subclasses, but then, while this may be a bit untidy, it doesn't really matter (if it is applied to another class, it will just not have any effect there, as no code ever processes it for other classes).