更好地处理枚举变更方法? [英] Better way to handle enum change methods?

查看:98
本文介绍了更好地处理枚举变更方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初,我的希望是使用枚举进行某种封装,但显然不是但可能与C#。我的目的是使用枚举来存储某些数据。理想情况下,拥有一个枚举的对象将会很棒。但是显然没办法这样做。所以我创建了一个状态类,一个公共枚举,并使用一个setter来创建一个public getter / setter,该setter可以初始化一个可以将对象作为属性填充的方法。
我想知道是否有一个更好的方法。对于我来说,理想的解决方案是设置一个正常的状态(枚举):

  car.State = CarStates.Idle; 

然后访问更多有关该状态的数据,如下所示:

  string message = car.State.Message(); 

但这将涉及将属性附加到状态枚举。有没有一个很酷的技巧来实现这个效果?枚举是您通过简单地添加 .Idle

$ b $的唯一方法,使可切换,单例类似的值b

这里的代码现在通过添加一个可以通过的层来保持状态信息在一个级别,但在声明类似于 car.Mode时感觉很多。 State = CarModeStates.Idle;

  class Car 
{
public CarState模式;

public Car()
{
Mode = new CarState();
}
}

class CarState //持有给定枚举值的任何元数据
{
private int _EnumInt;
私人字符串_Message;
private bool _IsError = false;

public CarModeStates状态
{
get {return(CarModeStates)_EnumInt; }
set {_EnumInt =(int)value; InitializeState(值); }
}
public string Message {get {return _Message; }}
public bool IsError {get {return _IsError;

public void InitializeState(CarModeStates?cs)
{
switch(cs)
{
case(CarModeStates.Off):
{
_Message =汽车关闭。
_IsError = false;
break;
}
case(CarModeStates.Idle):
{
_Message =汽车空转。
_IsError = false;
break;
}
case(CarModeStates.Drive):
{
_Message =汽车正在驾驶。
_IsError = false;
break;
}
case(CarModeStates.Accident):
{
_Message =CRASH!;
_IsError = true;
break;
}
}
}
}

public enum CarModeStates
{
关,
空闲,
驱动器,
事故,
}

//从外面:
class SomeController
{
汽车=新车();
public string GetStateMessage()
{
car.Mode.State = CarModeStates.Idle;

return car.Mode.Message;
}
}


解决方案

你试过一个扩展方法?在静态类中,定义:

  public static string Message(此CarStates值)
{
开关(价值){
...
}
}


Initially, my hope was to have some sort of encapsulation with enums, but apparently that isn't yet possible with C#. My purpose was to store some sort of data with the enum. Ideally, having an enum of objects would have been great. But there's apparently no way to do that. So I created a state class, a public enum, and made a public getter/setter with a setter that initializes a method in which i can populate objects as a property. I'm wondering if there's a slightly better way. The ideal solution for me would be to set a state (enum) the normal way:

car.State = CarStates.Idle;

and then access more data about that state like so:

string message = car.State.Message();

But this would involve attaching a property to the State enum. Is there any cool trick to achieve this effect? Are enums the only way you can make switchable, singleton-like values by simply adding .Idle to the end?

Here's the code as I have it now that keeps the state info at one level by adding a layer, which is passable, but feels redundant when declaring something like car.Mode.State = CarModeStates.Idle;.

class Car
{
    public CarState Mode;

    public Car()
    {
        Mode = new CarState();
    }
}

class CarState //holds any metadata for given enum value
{
    private int _EnumInt;
    private string _Message;
    private bool _IsError = false;

    public CarModeStates State
    { 
        get { return (CarModeStates)_EnumInt; } 
        set { _EnumInt = (int)value; InitializeState(value); } 
    }
    public string Message { get { return _Message; } }
    public bool IsError { get { return _IsError; } }

    public void InitializeState(CarModeStates? cs)
    {
        switch (cs)
        {
            case (CarModeStates.Off):
                {
                    _Message = "Car is off.";
                    _IsError = false;
                    break;
                }
            case (CarModeStates.Idle):
                {
                    _Message = "Car is idling.";
                    _IsError = false;
                    break;
                }
            case (CarModeStates.Drive):
                {
                    _Message = "Car is driving.";
                    _IsError = false;
                    break;
                }
            case (CarModeStates.Accident):
                {
                    _Message = "CRASH!";
                    _IsError = true;
                    break;
                }
        }
    }
}

public enum CarModeStates
{
    Off,
    Idle,
    Drive,
    Accident,
}

//And from the outside:
class SomeController
{
    Car car = new Car();
    public string GetStateMessage()
    {
        car.Mode.State = CarModeStates.Idle;

        return car.Mode.Message;
    }
}

解决方案

Have you tried an extension method? In a static class, define:

public static string Message( this CarStates value )
{
    switch (value) {
    ...
    }
}

这篇关于更好地处理枚举变更方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆