C#Enum忽略名称';秩序

C#Enum忽略名称';秩序,c#,enums,C#,Enums,我有下一个目标: private enum Operation { Power = 0x5E, // ^ Division = 0x2F, // / Multiplication = 0x2A, // * Subtraction = 0x2D, // - Addition = 0x2B // + } 当我想用下一种方法将其转换为char[

我有下一个目标:

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>();
}
我想要实现的:(因此数组的顺序与
enum
声明的顺序相同)

提前谢谢

数组元素按枚举常量的二进制值(即,按其无符号大小)排序

资料来源:

数组元素按枚举常量的二进制值(即,按其无符号大小)排序


来源:

您可以使用这些方法来维持秩序并关联一个值。请注意,代码是徒手编写的(未经测试)

公共价值属性:属性
{
公共字符值{get;set;}
public ValueAttribute(字符值){value=value;}
}
私有枚举操作
{
[值(0x5E)]
功率=1,
[值(0x2F)]
除法=2,
[值(0x2A)]
乘法=3,
[值(0x2D)]
减法=4,
[值(0x2B)]
加法=5,
}
私有静态字符[]GetOperators()
{
List ExistingOperators=new List();
foreach(Enum.GetValues中的操作枚举运算符(typeof(Operation)))
{
ExistingOperators.Add(enumOperator.Value());
Console.WriteLine(枚举运算符);
}
返回ExistingOperators.ToArray();
}
公共静态类扩展
{
公共静态字符值(此操作op)
{
var attr=typeof(操作)
.GetField(op.ToString())
.GetCustomAttributes(typeof(ValueAttribute),false)[0]
作为价值属性;
返回属性值;
}
}
使用索引属性进行替换:

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;
    }
}
公共索引属性:属性
{
公共int索引{get;set;}
公共索引属性(int-index){index=index;}
}
私有枚举操作
{
[索引(1)]
功率=0x5E,
[索引(2)]
除法=0x2F,
[索引(3)]
乘法=0x2A,
[索引(4)]
减法=0x2D,
[索引(5)]
加法=0x2B,
}
公共结构基准
{
int索引{get;set;}
字符值{get;set;}
操作Op{get;set;}
}
私有静态字符[]GetOperators()
{
IEnumerable数据=新列表();
foreach(Enum.GetValues中的操作枚举运算符(typeof(Operation)))
{
数据。添加(新数据)
{ 
Index=enumOperator.Index(),
Value=(char)枚举运算符,
Op=枚举运算符
});
}
//假设您可以使用LINQ
data=data.OrderBy(d=>d.Index);
data.Foreach(d=>Console.WriteLine(d=>d.Op));
返回数据。选择(d=>d.Value).ToArray();
}
公共静态类扩展
{
公共静态字符索引(此操作op)
{
var attr=typeof(操作)
.GetField(op.ToString())
.GetCustomAttributes(typeof(IndexAttribute),false)[0]
作为指数化指标;
返回属性索引;
}
}

您可以使用这些方法来维持秩序并关联一个值。请注意,代码是徒手编写的(未经测试)

公共价值属性:属性
{
公共字符值{get;set;}
public ValueAttribute(字符值){value=value;}
}
私有枚举操作
{
[值(0x5E)]
功率=1,
[值(0x2F)]
除法=2,
[值(0x2A)]
乘法=3,
[值(0x2D)]
减法=4,
[值(0x2B)]
加法=5,
}
私有静态字符[]GetOperators()
{
List ExistingOperators=new List();
foreach(Enum.GetValues中的操作枚举运算符(typeof(Operation)))
{
ExistingOperators.Add(enumOperator.Value());
Console.WriteLine(枚举运算符);
}
返回ExistingOperators.ToArray();
}
公共静态类扩展
{
公共静态字符值(此操作op)
{
var attr=typeof(操作)
.GetField(op.ToString())
.GetCustomAttributes(typeof(ValueAttribute),false)[0]
作为价值属性;
返回属性值;
}
}
使用索引属性进行替换:

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;
    }
}
公共索引属性:属性
{
公共int索引{get;set;}
公共索引属性(int-index){index=index;}
}
私有枚举操作
{
[索引(1)]
功率=0x5E,
[索引(2)]
除法=0x2F,
[索引(3)]
乘法=0x2A,
[索引(4)]
减法=0x2D,
[索引(5)]
加法=0x2B,
}
公共结构基准
{
int索引{get;set;}
字符值{get;set;}
操作Op{get;set;}
}
私有静态字符[]GetOperators()
{
IEnumerable数据=新列表();
foreach(Enum.GetValues中的操作枚举运算符(typeof(Operation)))
{
数据。添加(新数据)
{ 
Index=enumOperator.Index(),
Value=(char)枚举运算符,
Op=枚举运算符
});
}
//假设您可以使用LINQ
data=data.OrderBy(d=>d.Index);
data.Foreach(d=>Console.WriteLine(d=>d.Op));
返回数据。选择(d=>d.Value).ToArray();
}
公共静态类扩展
{
公共静态字符索引(此操作op)
{
var attr=typeof(操作)
.GetField(op.ToString())
.GetCustomAttributes(typeof(IndexAttribute),false)[0]
作为指数化指标;
返回属性索引;
}
}

与IComparer一起使用某种排序列表:枚举的顺序是按其值存储的,您可以按名称对它们进行排序(只需使用.OrderBy(x=>enum.GetName(typeof(Operation),x))。声明的顺序根本不保留。您可以做什么(如果您真的需要它的话…)是应用一些自定义属性并将其用于排序(类似于DisplayOrderAttribute)。更好的解决方案(用于将来的扩展和实际问题)是根本不使用枚举。创建一个带有私有构造函数和许多常量字段(加法、乘法等)的基类操作1.保留申报单
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;
    }
}
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;
    }
}