How can I make an “abstract” enum in a .NET class library?

后端 未结 7 420
谎友^
谎友^ 2020-12-29 10:17

I\'m making a server library in which the packet association is done by enum.

public enum ServerOperationCode : byte
{
    LoginResponse = 0x00,
    Selectio         


        
7条回答
  •  我在风中等你
    2020-12-29 10:41

    How about using static Dictionary and a virtual method to retrieve the static dictionaries in the inherited classes?

    Like the follow for your case:

        public abstract class Operation
        {
            protected abstract Dictionary getCodeTable();
            public int returnOpCode(string request){ return getCodeTable()[request]; }
        }
        public class ServerOperation : Operation
        {
            Dictionary serverOpCodeTable = new Dictionary()
            {
                {"LoginResponse", 0x00,},
                {"SelectionResponse", 0x01},
                {"BlahBlahResponse", 0x02}
            };
            protected override Dictionary getCodeTable()
            {
                return serverOpCodeTable;
            }
    
        }
        public class ClientOperation : Operation
        {
            Dictionary cilentOpCodeTable = new Dictionary()
            {
                {"LoginResponse", 0x00,},
                {"SelectionResponse", 0x01},
                {"BlahBlahResponse", 0x02}
            };
            protected override Dictionary getCodeTable()
            {
                return cilentOpCodeTable;
            }
        }
    

提交回复
热议问题