给定以下枚举:
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
};
然后检查IsDefined
一个适当的FieldInfo
. 语法不是很漂亮,但是您可以添加一些扩展方法来使事情更易于管理:
public static bool IsOffensivePosition(PositionType pt)
{
return typeof(PositionType).GetField(Enum.GetName(typeof(PositionType), pt)).
IsDefined(typeof(OffensivePositionAttribute), false);
}
您可以使用一个属性,例如CategoryAttribute
:
public enum Position
{
[Category("Offensive")]
Quarterback,
[Category("Offensive")]
Runningback,
[Category("Defensive")]
DefensiveEnd,
[Category("Defensive")]
Linebacker
};
为什么不亲吻:
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);
}
也许您可以尝试使用typesefe 枚举模式
class 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 };
}
您可以使用某种形式的标志位。但这可能会导致混乱。更好的方法可能是使用您想要的详细信息创建自定义类,然后使用 Dictionary 查找每种职位类型;
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 }
}
请注意,防御值从 10 开始,因此值不会重叠。您没有说明为什么要这样做,因此这可能无法满足您的需求。