简体   繁体   English

控件类型和代码的类或枚举结构

[英]Class or Enum structure for control types and codes

I would like to implement a class with control codes for the peers of my socket communication. 我想为套接字通信的对等体实现一个带有控制代码的类。 I tried it with enums and derived classes but my tries doesn't work. 我尝试了枚举和派生类,但我的尝试不起作用。

I would like to have a Type which contains different Codes . 我想要一个包含不同CodesType For example Control as type with Wakeup and Shutdown as code information's. 例如, 控件类型为WakeupShutdown为代码信息。

The peer should be able to use a Switch Case to identify the information. 对等方应该能够使用交换案例来识别信息。 Something like: 就像是:

switch(data.Type)
{
    case Control:
        /* Check diffrent codes */
    break;
    case Notification:
        /* Check diffrent codes */
    break;
}

I tried it with following, but sadly I cannot use Switch Case or similar to identify the information. 我尝试了以下方法,但遗憾的是我无法使用Switch Case或类似产品来识别信息。

public abstract class Type
{
    private readonly String _code;

    protected Type(String code)
    {
        _code = code;
    }

    public String Code
    {
        get { return _code; }
    }
}

public sealed class Control : Type
{
    public static readonly Control Wakeup = new Control("Wakeup");
    public static readonly Control Shutdown = new Control("Shutdown");

    private Control(String code) : base(code)
    {
    }
}

Any Idea how to implement this? 任何想法如何实现这一点? Please consider, that TypeXXX should not combinable with CodeYYYY . 请考虑, TypeXXX不能与CodeYYYY结合使用。 Thanks for your advices! 感谢您的建议!

Edit 编辑

I have a TCP Client and Server Socket. 我有一个TCP客户端和服务器套接字。 The general communication works fine. 一般通讯正常。 Now I would like to implement a class or something similar that tells the peer which kind of information he received. 现在,我想实现一个类或类似的东西,以告诉同龄人他收到了哪种信息。 That he is able to decide what he should he do with the information. 他能够决定如何处理该信息。 I thought something like the ICMP Control Messages would be nice. 我认为类似ICMP控制消息之类的东西会很好。 I have a general Type of Information and the Code specify it. 我有一个通用的信息类型,代码指定了它。

The type code relationship looks like this: 类型代码关系如下所示:

+--------------+-------------+
|     Type     |    Code     |
+--------------+-------------+
| Control      | Shutdown    |
|              | Wakeup      |
|              | CheckUpdate |
|              | ForceUpdate |
| -----------  | ----------- |
| Notification | Online      |
|              | Offline     |
|              | Login       |
|              | Logoff      |
| -----------  | ----------- |
| Data         | Zip         |
+--------------+-------------+

The package looks like this: 该软件包如下所示:

+------+------+---------+
| Type | Code | Payload |
+------+------+---------+

The receiver checks the Type and Code and after that start to process the payload. 接收器检查TypeCode ,然后开始处理有效负载。

Create two classes that inherits from Control class: 创建两个从Control类继承的类:

public class WakeUp : Control
{
}

public class ShutDown : Control
{
}

Then use this method to create a new instance of the specific control: 然后使用此方法创建特定控件的新实例:

public T CreateControl<T>() where T : Control, new()
{
    return new T();
}

Use the above method as: 使用上面的方法:

var wakeupControl = CreateControl<WakeUp>();
var shutdownControl = CreateControl<ShutDown>();

UPDATE: 更新:

Have a look at the strategy pattern http://www.dofactory.com/Patterns/PatternStrategy.aspx 看看策略模式http://www.dofactory.com/Patterns/PatternStrategy.aspx

From C# language reference (8.7.2). 从C#语言参考(8.7.2)。 switch (switch expression) { case... } 开关(开关表达式){case ...}

If the type of the switch expression is sbyte, byte, short, ushort, int, uint, long, ulong, char, string, or an enum-type , then that is the governing type of the switch statement. 如果switch表达式的类型是sbyte,byte,short,ushort,int,uint,long,ulong,char,string或enum-type ,则这就是switch语句的控制类型。 Otherwise, exactly one user-defined implicit conversion (Section 6.4) must exist from the type of the switch expression to one of the following possible governing types: sbyte, byte, short, ushort, int, uint, long, ulong, char, string . 否则,从switch表达式的类型到以下可能的控制类型之一,必须存在一个用户定义的隐式转换(第6.4节): sbyte,byte,short,ushort,int,uint,long,ulong,char,string If no such implicit conversion exists, or if more than one such implicit conversion exists, a compile-time error occurs. 如果不存在这样的隐式转换,或者存在多个这样的隐式转换,则会发生编译时错误。

I think that you are overcomplicating. 我认为您太复杂了。 This is how I would do it. 这就是我要做的。

public enum Code { Wakeup, ShutDown, Notification1, Notification2 };
public enum Type { Control, Notification, Invalid }

public static class CodeCheck
{
    public static bool IsControl( Code code )
    {
        return code == Code.Wakeup || code == Code.ShutDown;
    }

    public static bool IsNotification( Code code )
    {
        return code == Code.Notification1 || code == Code.Notification2;
    }

    public static Type GetType( Code code )
    {
        if ( IsControl( code ) )
            return Type.Control;

        if ( IsNotification( code ) )
            return Type.Notification;

        return Type.Invalid;
    }
}

So user would do something like: 因此用户将执行以下操作:

    public void ProcessCode( Code code )
    {
        switch ( CodeCheck.GetType( code ) )
        {
            case Type.Control:
                // do something
                break;

            case Type.Notification:
                // do something
                break;
        }
    }

Here is another version of how you can do it. 这是您如何做的另一种形式。 This is after your edit so it covers all combinations: 这是在您编辑后的,因此涵盖了所有组合:

public enum CodeValue { Wakeup, ShutDown, CheckUpdate, ForceUpdate, Online, Offline, Login, Logoff, Zip };
public enum TypeValue { Control, Notification, Data, Invalid }

public class Code
{
    public CodeValue CodeValue
    {
        get;
        private set;
    }

    public TypeValue TypeValue
    {
        get;
        private set;
    }

    public Code( CodeValue code )
    {
        CodeValue = code;
        DetermineType();
        if ( TypeValue == TypeValue.Invalid )
            throw new ArgumentException( "code" );
    }


    private bool IsControl()
    {
        return CodeValue == CodeValue.Wakeup || CodeValue == CodeValue.ShutDown || CodeValue == CodeValue.CheckUpdate || CodeValue == CodeValue.ForceUpdate;
    }

    private bool IsNotification()
    {
        return CodeValue == CodeValue.Online || CodeValue == CodeValue.Offline || CodeValue == CodeValue.Login || CodeValue == CodeValue.Logoff;
    }

    private bool IsData()
    {
        return CodeValue == CodeValue.Zip;
    }

    private void DetermineType()
    {
        if ( IsControl() )
            TypeValue = TypeValue.Control;

        if ( IsNotification() )
            TypeValue = TypeValue.Notification;

        if ( IsData() )
            TypeValue = TypeValue.Data;

        TypeValue = TypeValue.Invalid;
    }
}

User would use it like this: 用户可以这样使用它:

    public void ProcessCode( Code code )
    {
        switch ( code.TypeValue )
        {
            case TypeValue.Control:
                // do something
                break;

            case TypeValue.Notification:
                // do something
                break;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM