更好地处理枚举变更方法? [英] Better way to handle enum change methods?
问题描述
我想知道是否有一个更好的方法。对于我来说,理想的解决方案是设置一个正常的状态(枚举):
car.State = CarStates.Idle;
然后访问更多有关该状态的数据,如下所示:
string message = car.State.Message();
但这将涉及将属性附加到状态枚举。有没有一个很酷的技巧来实现这个效果?枚举是您通过简单地添加 .Idle
这里的代码现在通过添加一个可以通过的层来保持状态信息在一个级别,但在声明类似于 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屋!