Given the following enum:
public enum Position
{
Quarterback,
Runningback,
DefensiveEnd,
Linebacker
};
Is it possible to classify the named constants, such that I could mark 'Quarterback' and 'Runningback' as offensive positions and 'DefensiveEnd' and 'Linebacker' as defensi开发者_运维知识库ve positions?
You can use attributes:
public enum Position
{
[OffensivePosition]
Quarterback,
[OffensivePosition]
Runningback,
[DefensivePosition]
DefensiveEnd,
[DefensivePosition]
Linebacker
};
And then check for IsDefined
on an appropriate FieldInfo
. Syntax is not very pretty, but you can throw in a couple of extension methods to make things more manageble:
public static bool IsOffensivePosition(PositionType pt)
{
return typeof(PositionType).GetField(Enum.GetName(typeof(PositionType), pt)).
IsDefined(typeof(OffensivePositionAttribute), false);
}
Why not 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;
}
}
}
You could use an attribute, like CategoryAttribute
:
public enum Position
{
[Category("Offensive")]
Quarterback,
[Category("Offensive")]
Runningback,
[Category("Defensive")]
DefensiveEnd,
[Category("Defensive")]
Linebacker
};
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
You could use Flags
[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);
}
Maybe you can try to use typesefe enum pattern
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 };
}
You could use some form of flag bits. But that could lead to a mess. A better way may be to just create custom classes with the details you want and then use a Dictionary to lookup each position type;
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; }
}
... as enum ...
[Flags]
public enum Positions {
Quarterback = 0x21,
Runningback = 0x22,
DefensiveEnd = 0x14,
Linebacker = 0x18,
Defensive = 0x10,
Offsensive = 0x20
}
An underutilized (but perfectly valid) technique is to use a class which defines a set of constants. As a class, you can add additional properties that can describe other aspects of the enumerated value. Curiously, this is the way most enums are implemented in Java (which doesn't have a special keyword for them).
If you go this route, it's generally a good idea to make the class sealed and define a private constructor, so that only the class itself can define instances. Here's an example:
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...
}
Using such an enum results in more elegant and simpler syntax than attributes:
if( PlayerPosition.Quarterback.IsDefensive )
{
// ...
}
You can declare the enums in a class:
public class Position
{
public enum Offensive { Quarterback = 1, RunningBack }
public enum Defensive { DefensiveEnd = 10, LineBacker }
}
Note that the Defensive values start at 10 so that values don't overlap. You don't state why you want to do this, so this might not meet your needs.
精彩评论