I have many classes that I have custom serialization methods for. They all follow this pattern (Bits
is a wrapper for a byte[] that also has custom serialization methods):
public class Foo
{
...
props
...
public void Write(Bits bits)
{
... custom serialization ...
}
public static Foo Read(Bits bits)
{
... custom deserialization ...
}
}
It would be really nice to be able to turn this pattern into an interface. However it doesn't seem to be possible with the static Read
method. I don't want to turn it to a constructor for multiple reasons. I know that C# 8 introduces default interface methods that can be static, but that would not enforce Read
to be implemented and I would need to reference the specific implementer class.
Do I have any other options? I really like that when you use an interface in Visual Studio you get the 'Quick Action' option to implement all the interface methods.