C#Enum忽略了名称的顺序

时间:2012-10-07 11:55:36

标签: c# enums

我有下一个目标:

private enum Operation
{
        Power = 0x5E,             // ^
        Division = 0x2F,          // /
        Multiplication = 0x2A,    // *
        Subtraction = 0x2D,      // -
        Addition = 0x2B           // +
}

当我想以下一种方式将其转换为char[]时:

private static char[] GetOperators()
{
        List<char> ExistingOperators = new List<char>();

        foreach (Operation enumOperator in Enum.GetValues(typeof(Operation)))
        {
                ExistingOperators.Add((char)enumOperator);
                Console.WriteLine(enumOperator);
        }

        return ExistingOperators.ToArray<char>();
}

它在控制台上写入枚举的值,但是从小到大排序。 以上示例输出:

Multiplication
Addition
Subtraction
Divison
Power

我想要实现的目标:(因此数组的顺序与enum声明的顺序相同)

char[] { '^', '/', '*', '*', '-', '*' };

提前致谢。

2 个答案:

答案 0 :(得分:5)

  

数组的元素按枚举常量的二进制值(即无符号幅度)排序。

来源:http://msdn.microsoft.com/en-us/library/system.enum.getvalues.aspx

答案 1 :(得分:2)

您可以使用这些行中的内容来维护顺序并关联值。请注意,代码是徒手的(未经测试)。

public ValueAttribute : Attribute
{
    public char Value { get; set; }
    public ValueAttribute(char value) { Value = value; }
}

private enum Operation
{
    [Value(0x5E)]
    Power = 1,
    [Value(0x2F)]
    Division = 2,
    [Value(0x2A)]
    Multiplication = 3,
    [Value(0x2D)]
    Subtraction = 4,
    [Value(0x2B)]
    Addition = 5,
}

private static char[] GetOperators()
{
    List<char> ExistingOperators = new List<char>();

    foreach (Operation enumOperator in Enum.GetValues(typeof(Operation)))
    {
            ExistingOperators.Add(enumOperator.Value());
            Console.WriteLine(enumOperator);
    }

    return ExistingOperators.ToArray<char>();
}

public static class Extensions
{
    public static char Value(this Operation op)
    {
        var attr = typeof(Operation)
            .GetField(op.ToString())
            .GetCustomAttributes(typeof(ValueAttribute), false)[0]
            as ValueAttribute;
        return attr.Value;
    }
}

使用IndexAttribute替代:

public IndexAttribute : Attribute
{
    public int Index { get; set; }
    public IndexAttribute(int index) { Index = index; }
}

private enum Operation
{
    [Index(1)]
    Power = 0x5E,
    [Index(2)]
    Division = 0x2F,
    [Index(3)]
    Multiplication = 0x2A,
    [Index(4)]
    Subtraction = 0x2D,
    [Index(5)]
    Addition = 0x2B,
}

public struct Datum
{
    int Index { get; set; }
    char Value { get; set; }
    Operation Op { get; set; }
}
private static char[] GetOperators()
{
    IEnumerable<Datum> data = new List<Datum>();

    foreach (Operation enumOperator in Enum.GetValues(typeof(Operation)))
    {
        data.Add(new Datum 
        { 
            Index = enumOperator.Index(), 
            Value = (char)enumOperator,
            Op = enumOperator
        });
    }

    // assuming you can use LINQ
    data = data.OrderBy(d => d.Index);
    data.Foreach(d => Console.WriteLine(d => d.Op));

    return data.Select(d => d.Value).ToArray();
}

public static class Extensions
{
    public static char Index(this Operation op)
    {
        var attr = typeof(Operation)
            .GetField(op.ToString())
            .GetCustomAttributes(typeof(IndexAttribute), false)[0]
            as IndexAttribute;
        return attr.Index;
    }
}