10

I am currently developping a library for a binary application protocol. The following code sample does not compile (static methods definition not allowed in abstract type. Too bad :(), but shows my intention :

public abstract class CmdBody<T>
{
    public static abstract byte[] ToBytes(T cmdBody);
    public abstract int GetLength();
}

public class CmdBodyA : CmdBody<CmdBodyA>
{
    static override byte[] ToBytes(CmdBodyA cmdBody) 
    { // Implementation }
}

public class CmdBodyB : CmdBody<CmdBodyB>
{
    static override byte[] ToBytes(CmdBodyB cmdBody)
    { // Implementation }
}

[...]

public class Cmd<T>
{ 
    public CmdHeader Header { get; set; }
    public CmdBody<T> Body { get; set; }

    public byte[] ToBytes()
    {
        byte[] cmdBytes = new byte[Header.GetLength() + Body.GetLength()];
        Header.ToBytes().CopyTo(cmdBytes, 0);
        Body.ToBytes().CoptyTo(cmdBytes, Header.GetLength());

        return cmdBytes;
    } 
}

So pretty basic stuff, a Cmd consists of an Header and a Body, Header type being common to all Cmd(s) and Body having different parameters (properties) depending of the Cmd type, and I would like to work with Cmd objects, and to be able to call ToBytes() on them to send them over the network.

In my real implementation I am using conversion operator instead of ToBytes() method, but I wanted to keep the code sample simple, at the end it does the same job.

I have many different command types, and I can't work out a solution to keep it simple and achieving what I want with just a single generic Cmd type. The only solution I can think of would be to get rid of the static method in the base CmdBody class, get rid of the generic aspect and for each CmdBody type make an associated Cmd class (class CmdA, class CmdB ...) but this will incur a lot of code duplication at the end of the day.

Any idea for a nice desing to help me work arround this ?

darkey
  • 3,672
  • 3
  • 29
  • 50
  • Is it possible you're looking for an interface, rather than an abstract type? An abstract type is used to define classes that inherit from it, where an interface is used to define required methods and fields for a class to implement. – Codeman Aug 03 '12 at 18:25
  • Doesn't matter here. Interface or abstract classes can't define static methods, the design problem would remain. – darkey Aug 03 '12 at 18:26
  • =/ You cannot define static methods without implementation on abstract classes. (A static member cannot be marked as override, virtual, or abstract) – marcoaoteixeira Aug 03 '12 at 18:27
  • @Pheonixblade9 he does want an contract (interface), but saying 'to derive you also have to provide a static method ToBytes'. – payo Aug 03 '12 at 18:28
  • It makes sense you can't define a static stub - static methods (or variables) are singular per Class i.e. its inheritable but not override able. You should define the implementation in the abstract class. By the looks of it you are using generics which should allow you the ability to pass in any type and you would use generic contraints to define what types T should be. – Paul Sullivan Aug 03 '12 at 18:30
  • Why are you trying to do this via a static method in the first place? Your API doesn't require static anywhere to work... – Reed Copsey Aug 03 '12 at 18:30
  • See http://stackoverflow.com/questions/3284/why-cant-i-have-abstract-static-methods-in-c – Jon Senchyna Aug 03 '12 at 18:32
  • Possible duplicate of [Why can't I have abstract static methods in C#?](https://stackoverflow.com/questions/3284/why-cant-i-have-abstract-static-methods-in-c) – Ken Kin Jan 10 '18 at 04:56
  • Possible duplicate of [How to implement virtual static properties?](https://stackoverflow.com/questions/15346631/how-to-implement-virtual-static-properties) – peterh Jan 10 '18 at 11:12

1 Answers1

13

You can't have "virtual" static methods, as virtual/abstract/override suggests an instance being overridden at runtime.

You could do something like this, however:

public abstract class CmdBody
{
    public static byte[] ToBytes<T>(T cmdBody) where T : CmdBody
    {
         return cmdBody.ToBytes();
    }

    protected abstract byte[] ToBytes();

    public abstract int GetLength();
}

public class CmdBodyA : CmdBody
{
    protected override byte[] ToBytes() 
    { // Implementation }
}

public class CmdBodyB : CmdBody
{
    protected override byte[] ToBytes() 
    { // Implementation }
}

This allows each "CmdBody" type to define how it's serialized itself, but the base class static method to be the only publicly visible way to access it.

That being said, given your use case, there's no reason for making this static at all. You can just do:

public abstract class CmdBody
{
    public abstract byte[] ToBytes();

    public abstract int GetLength();
}

public class CmdBodyA : CmdBody
{
    public override byte[] ToBytes() 
    { // Implementation }
}

public class CmdBodyB : CmdBody
{
    public override byte[] ToBytes() 
    { // Implementation }
}


public class Cmd<T> where T : CmdBody
{ 
    public CmdHeader Header { get; set; }
    public T Body { get; set; }

    public byte[] ToBytes()
    {
        byte[] cmdBytes = new byte[Header.GetLength() + Body.GetLength()];
        Header.ToBytes().CopyTo(cmdBytes, 0);
        Body.ToBytes().CopyTo(cmdBytes, Header.GetLength());

        return cmdBytes;
    } 
}
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Woooo ! Very nice work arround in your first code sample. Sexy stuff :) And thanks a lot for your second proposal (you just should edit it to remove one of the ToBytes() definition in CmdBody). Thanks a lot ! – darkey Aug 03 '12 at 18:35
  • @darkey Fixed the ToBytes things... – Reed Copsey Aug 03 '12 at 19:31