Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8332467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:43:57+00:00 2026-06-09T02:43:57+00:00

I am currently developping a library for a binary application protocol. The following code

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:43:59+00:00Added an answer on June 9, 2026 at 2:43 am

    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;
        } 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently developping a JavaEE application using glassfish, and I would like to save
I'm currently in the process of developing a fairly large static library which will
currently, I am developing a J2ME application for mobiles using LWUIT library. The build
I'm currently developping a commercial Windows application (closed-source, free demo with limited functionality available)
I am currently developing an Android-App in Eclipse and trying to use a Library
I am currently developing a small simulation utility, using the Task Parallel Library to
I'm currently developping an expression method (used in linq to entity queries) who has
I'm currently developping a search engine using Solr for an ecommerce website. So I
Currently developing an application using the newest version of symfony, obtained through PEAR. This
Im currently developing an In-House Enterprise application. I will publish the app using Apple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.