In C# it is not possible to override a static method. Nevertheless I have some type specific methods (instead of the usual instance methods) and I want to give the user the ability to override those methods, and in addition, have the ability to call those (overridden) methods from my own code without knowing about the 'overriding' done by the user. Would the method be static, I would have to know the type name of the user's class in advance, which is not what I want.
How to implement type specific methods like Read
, Write
and GetLength
and allow overriding?
Background
There is an abstract Row
class whose derived types/classes represents a type of row and whose instance represents the actual row and its fields/data in a very simple table. Each type of row has a fixed length and the table is just a head-to-tail run of rows in a file. The Row
class needs three methods: Read
and Write
methods which perform their obvious function on a stream given an offset, and a GetLength
method which returns the fixed length of the type of row.
Now the user may extend my Row
class and provide implementations for Read
, Write
and GetLength
for his or her specific row type, and fields and properties to be used in his or her specific row instance. For example, the SomeUserRow
class may have a 32-bit integer field and a single byte field, a fixed length of 5 bytes and corresponding read and write method implementation.
The methods
Read
An obvious factory method, related to the type and therefore I would define it in the class itself. I'd make it static
, but then it cannot be overridden. Then I could make it protected abstract
and create a static generic Read<T>
method to call it, as suggested in this post. But I also need to be able to call this method from my code without knowing the type of the user implemented class. I can't just call Row.Read<UserType>()
because I don't know about the user's types yet.
Write
A possible instance method, because most people want to write an existing Row
to the stream. But having Read
static, it seems weird to make Write
an instance method.
GetLength
Not a factory method, but still related to the type. Again, I would make it static but this prevents overriding. I can choose to make it an instance method, which can be overridden but it feels wrong to do that in an Object Oriented environment: creating an instance just to get a value which does not depend on the instance (int length = new T().GetLength()
) but rather on its type.
I have also been thinking about moving the three methods out of the classes into a separate class, but that still does not address the overriding. Or to have a class which keeps a list or dictionary of delegates pointing to the correct methods. It does not allow for real overriding (replacing a method pointer in a delegate array is not what I'd consider true overriding) and it moves the type specific methods away from the type which I think is not good from a developers point of view: having two or more places to change when you just want to change the type.
Via reflection it is possible to call the correct static method on a type, but as I was reading many rows, I found it to be too slow.