As far as I know C#
doesn't support virtual static properties. How to implement such a behavior in C#
?
I want to archive that all derived classes of a base class must override a static property. Getting a derived type, I want to access to a static property called Identifier
Type t = typeof(DerivedClass);
var identifier= (String) t.GetProperty("Identifier", BindingFlags.Static).GetValue(null, null);
For you still don't have an accpted answer about five years later, let me give it a try(again) ..
I've ever thought about the Curiously Recurring Template Pattern as a workaround, but since you'll open
BaseClass
for inheritance it would not be a good idea. You might want to have a look at Mr. Lippert's blogpost for a better understanding of why.Solution 1: You don't register, I don't recognize ..
test:
This is is a relatively simple pattern through the type initializer. The
Register
method is only exposed to derived class; and both theGetIdentifier
andRegister
methods are constrained to be invoked with a type argument which is derived fromBaseClass
. Although we don't force the derived classes to override anything, if it doesn't register itself,GetIdentifier
doesn't recognize it and returnsnull
.Solution 2: Before you show your identity, I buy you a default. Whoever you think you are, I believe -- as long as there are no ambiguity.
and the test:
Apparently this is a more complicated solution. As you can see
idBeforeInstantiation
andidAfterInstantiation
are different, however, they are either valid identifiers forDerivedClassA
.m_identities
contains the last updated identifier for each derived class andm_aliases
will contain all the identifier aliases for the derived classes. Since the combination of virtual and static is not a feature of the language currently(maybe never ..), if we want enforce the override then we have to do it through some workaround. If you'll choose solution2, You might want to implemenet you ownUpdateAlias
to prevent the derived classes from providing too much of various aliases for a single type, though they will all be valid. The last statement in the test are put deliberately to demonstrate the conflict of identifiers.For these two solutions are carefully designed for your consideration of not to instantiate the derived classes, none of them requires that.
Simply put, you can't, so I humbly suggest that you leave it and try something else.
Please see the answer in this SO post. If you could implement such a feature you would have serious problems with inheritance.
Been there, done that. After I came to my senses again, I went for a regular inheritance approach. I think you should probably do the same.
Another way to do this that doesn't require a class to be registered but does require a little extra work is to create a static class that holds the "static" data for each derived class type and return a constant/static value from the static class. Let me explain the specifics of this approach.
One big reason for having a static property that is always the same for every member of the class is to avoid unnecessary memory use and repetition. While the method demonstrated here doesn't avoid this entirely, it still bypasses most of the "extra" overhead. The only use case that the example below will not satisfy is if the reason for using a static property is so that you don't have to have an instance since you must have an instance to get to the data.
If you need a virtual field or property that is always the same for each member of the class (static), use a non-static property that returns a "constant" or static data like this: