You can use enum with attributes:
public enum Action{
[MyValue("JUMP", 1)]
JUMP,
[MyValue("CROUCH", 2)]
CROUCH
}
[AttributeUsage(
AttributeTargets.Field |
AttributeTargets.Method |
AttributeTargets.Property,
AllowMultiple = true)]
public class MyValueAttribute : System.Attribute{
public string Value{get; private set}
public string AnimationId{get; private set;}
public MyValueAttribute(string animationId, string value){
AnimationId = animationId;
Value = value;
}
and you can get value as follows:
public static class EnumExtensions{
public static string GetValue(this Enum value)
{
var type = value.GetType();
var name = Enum.GetName(type, value);
if (name == null) return string.Empty;
var field = type.GetField(name);
if (field == null) return string.Empty;
var attr = Attribute.GetCustomAttribute(field, typeof(MyValueAttribute)) as MyValueAttribute;
return attr != null ? attr.Value: string.Empty;
}
public static string GetAnimationId(this Enum value)
{
var type = value.GetType();
var name = Enum.GetName(type, value);
if (name == null) return string.Empty;
var field = type.GetField(name);
if (field == null) return string.Empty;
var attr = Attribute.GetCustomAttribute(field, typeof(MyValueAttribute)) as MyValueAttribute;
return attr != null ? attr.AnimationId: string.Empty;
}
}
Usage:
Action.JUMP.GetValue();
Action.JUMP.GetAnimationId();
or you can use one method which return for example Tuple
with AnimationId
and Value