以下是给定的枚举类型:
public enum Position
{
Quarterback,
Runningback,
DefensiveEnd,
Linebacker
};
是否有可能将命名常量分类,使得我可以将“Quarterback”和“Runningback”标记为进攻位置,“DefensiveEnd”和“Linebacker”标记为防守位置?
以下是给定的枚举类型:
public enum Position
{
Quarterback,
Runningback,
DefensiveEnd,
Linebacker
};
是否有可能将命名常量分类,使得我可以将“Quarterback”和“Runningback”标记为进攻位置,“DefensiveEnd”和“Linebacker”标记为防守位置?
public enum Position
{
[OffensivePosition]
Quarterback,
[OffensivePosition]
Runningback,
[DefensivePosition]
DefensiveEnd,
[DefensivePosition]
Linebacker
};
然后检查适当的FieldInfo
上是否有IsDefined
。语法不太美观,但可以添加一些扩展方法使事情更易于管理:
public static bool IsOffensivePosition(PositionType pt)
{
return typeof(PositionType).GetField(Enum.GetName(typeof(PositionType), pt)).
IsDefined(typeof(OffensivePositionAttribute), false);
}
IsOffensivePosition
的实现。尽管如此,这可能不是我个人会选择的解决方案。 - ThorarinCategoryAttribute
:public enum Position
{
[Category("Offensive")]
Quarterback,
[Category("Offensive")]
Runningback,
[Category("Defensive")]
DefensiveEnd,
[Category("Defensive")]
Linebacker
};
为什么不使用KISS原则:
class PlayerPosition {
public enum Position {
Quarterback,
Runningback,
DefensiveEnd,
Linebacker
}
public enum Type {
Offense,
Defense
}
public static Type GetTypeForPosition(Position position) {
switch (position) {
case Quarterback:
case Runningback:
return Type.Offense;
case DefensiveEnd:
case Linebacker:
return Type.Defense;
}
}
}
public enum PositionType
{
Offensive,
Defensive,
}
public class PositionTypeAttribute : Attribute
{
public PositionTypeAttribute(PositionType positionType)
{
PositionType = positionType;
}
public PositionType PositionType { get; private set; }
}
public enum Position
{
[PositionType(PositionType.Offensive)]
Quarterback,
[PositionType(PositionType.Offensive)]
Runningback,
[PositionType(PositionType.Defensive)]
DefensiveEnd,
[PositionType(PositionType.Defensive)]
Linebacker
};
public static class PositionHelper
{
public static PositionType GetPositionType(this Position position)
{
var positionTypeAttr = (PositionTypeAttribute)typeof(Position).GetField(Enum.GetName(typeof(Position), position))
.GetCustomAttributes(typeof(PositionTypeAttribute), false)[0];
return positionTypeAttr.PositionType;
}
}
Position position1 = Position.Runningback;
Console.WriteLine(position1.GetPositionType()); //print: Offensive
Position position2 = Position.Linebacker;
Console.WriteLine(position2.GetPositionType()); //print: Defensive
[Flags]
public enum Position
{
Quarterback = 1,
Runningback = 2,
DefensiveEnd = 4,
Linebacker = 8,
OffensivePosition = Quarterback | Runningback,
DefensivePosition = Linebacker | DefensiveEnd,
};
//strictly for example purposes
public bool isOffensive(Position pos)
{
return !((pos & OffensivePosition) == pos);
}
[Flags]
时,你应该将枚举常量指定为二的幂。 - Thorarinclass Position
{
public bool Offensive { get; private set; }
public bool Defensive { get; private set; }
private Position()
{
Offensive = false;
Defensive = false;
}
public static readonly Position Quarterback = new Position() { Offensive = true };
public static readonly Position Runningback = new Position() { Offensive = true };
public static readonly Position DefensiveEnd = new Position() { Defensive = true };
public static readonly Position Linebacker = new Position() { Defensive = true };
}
你可以使用某种标志位的形式。但这可能会导致混乱。更好的方法可能是创建具有所需细节的自定义类,然后使用字典来查找每个位置类型。
public class PlayerPosition {
public PlayerPosition (string positionName, bool isDefensive ) {
this.Name = positionName;
this.IsDefensive = isDefensive ;
}
public string Name { get; private set; }
public bool IsDefensive { get; private set; }
}
...作为枚举类型...
[Flags]
public enum Positions {
Quarterback = 0x21,
Runningback = 0x22,
DefensiveEnd = 0x14,
Linebacker = 0x18,
Defensive = 0x10,
Offsensive = 0x20
}
一个被低估(但完全有效)的技巧是使用定义一组常量的类。作为一个类,您可以添加其他描述枚举值其他方面的属性。有趣的是,这是Java中实现大多数枚举的方式(它没有专门的关键字)。
如果您选择这种方法,通常最好将类封闭并定义私有构造函数,以便只有类本身可以定义实例。以下是一个示例:
public static class Position
{
private PlayerPosition (string name, bool isDefensive ) {
this.Name = name
this.IsDefensive = isDefensive ;
}
// any properties you may need...
public string Name { get; private set; }
public bool IsDefensive { get; private set; }
public bool IsOffensive { get { return !IsDefensive; } }
// static instances that act like an enum
public static readonly Quarterback = new PlayerPosition( "Quarterback", false );
public static readonly Runningback = new PlayerPosition( "Runningback", false );
public static readonly Linebacker = new PlayerPosition( "Linebacker", true );
// etc...
}
if( PlayerPosition.Quarterback.IsDefensive )
{
// ...
}
public class Position
{
public enum Offensive { Quarterback = 1, RunningBack }
public enum Defensive { DefensiveEnd = 10, LineBacker }
}