接受枚举项并返回枚举值(不是索引)的 C# 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/688036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 13:33:00  来源:igfitidea点击:

C# function that accepts an Enum item and returns the enum value (not the index)

c#functionenumsgenerics

提问by

say I have the following declarations:

说我有以下声明:

public enum Complexity { Low = 0, Normal = 1, Medium = 2,  High = 3 }
public enum Priority { Normal = 1, Medium = 2,  High = 3, Urgent = 4 }

and I want to code it so that I could get the enum value (not the index, like I earlier mentioned):

我想对它进行编码,以便我可以获得枚举值(不是索引,就像我之前提到的那样):

//should store the value of the Complexity enum member Normal, which is 1
int complexityValueToStore = EnumHelper.GetEnumMemberValue(Complexity.Normal); 
//should store the value 4
int priorityValueToStore = EnumHelper.GetEnumMemberValue(Priority.Urgent); 

How should this reusable function look like?

这个可重用的函数应该是什么样子的?

tia! -ren

蒂亚!人

采纳答案by Jon Skeet

Revised answer (after question clarification)

修改后的答案(问题澄清后)

No, there's nothing cleaner than a cast. It's more informative than a method call, cheaper, shorter etc. It's about as low impact as you could possibly hope for.

不,没有什么比演员表更干净的了。它比方法调用提供更多信息、更便宜、更短等。它的影响与您可能希望的一样低。

Note that if you wanted to write a generic method to do the conversion, you'd have to specify what to convert it to as well: the enum could be based on byteor longfor example. By putting in the cast, you explicitly say what you want to convert it to, and it just does it.

请注意,如果您想编写一个通用方法来进行转换,您还必须指定要将其转换为什么:枚举可以基于bytelong例如。通过放入演员表,你明确地说出你想把它转换成什么,它就是这样做的。

Original answer

原答案

What do you mean by "index" exactly? Do you mean the numeric value? Just cast to int. If you mean "position within enum" you'd have to make sure the values are in numeric order (as that's what Enum.GetValuesgives - not the declaration order), and then do:

“索引”究竟是什么意思?你是说数值吗?只需投射到int. 如果您的意思是“枚举中的位置”,则必须确保值按数字顺序排列(因为这是Enum.GetValues给出的 - 而不是声明顺序),然​​后执行:

public static int GetEnumMemberIndex<T>(T element)
    where T : struct
{
    T[] values = (T[]) Enum.GetValues(typeof(T));
    return Array.IndexOf(values, element);
}

回答by Smashery

You can find the integer value of an enum by casting:

您可以通过强制转换找到枚举的整数值:

int complexityValueToStore = (int)Complexity.Normal;

回答by Reed Copsey

If you want the value, you can just cast the enum to int. That would set complexityValueToStore == 1 and priorityValueToStore == 4.

如果你想要这个值,你可以将枚举转换为 int。这将设置 complexValueToStore == 1 和 priorityValueToStore == 4。

If you want to get the index (ie: Priority.Urgent == 3), you could use Enum.GetValues, then just find the index of your current enum in that list. However, the ordering of the enum in the list returned may not be the same as in your code.

如果您想获取索引(即:Priority.Urgent == 3),您可以使用Enum.GetValues,然后只需在该列表中找到您当前枚举的索引。但是,返回的列表中枚举的顺序可能与您的代码中的顺序不同。

However, the second option kind of defeats the purpose of Enum in the first place - you're trying to have discrete values instead of lists and indices. I'd rethink your needs if that is what you want.

但是,第二种选择首先违背了 Enum 的目的 - 您试图使用离散值而不是列表和索引。如果这是您想要的,我会重新考虑您的需求。

回答by Mark Maxham

I realize this isn't what you asked, but it's something you might appreciate.

我知道这不是您要问的,但您可能会欣赏它。

I discovered that you can find the integer value of an enum without a cast, if you know what the enum's minimum value is:

我发现如果你知道枚举的最小值是什么,你可以在没有强制转换的情况下找到枚举的整数值:

public enum Complexity { Low = 0, Normal = 1, Medium = 2,  High = 3 }

int valueOfHigh = Complexity.High - Complexity.Low;

This wouldn't work with Priority, unless you added some minimal value of 0, or added 1 back:

这不适用于 Priority,除非您添加了一些最小值 0,或者添加了 1:

public enum Priority { Normal = 1, Medium = 2,  High = 3, Urgent = 4 }

int valueOfUrgent = Priority.Urgent - Priority.Normal + 1;

I find this technique much more aesthetically appealing than casting to int.

我发现这种技术比转换为 int 更具美学吸引力。

I'm not sure off the top of my head what happens if you have an enum based on byte or long -- I suspect that you'd get byte or long difference values.

我不确定如果你有一个基于字节或长的枚举会发生什么 - 我怀疑你会得到字节或长差异值。

回答by Nathan Baulch

The most generic way I know of is to read the value__field using reflection. This approach makes no assumptions about the enum's underlying type so it will work on enums that aren't based on Int32.

我所知道的最通用的方法是value__使用反射读取字段。这种方法不对枚举的底层类型做任何假设,因此它适用于不基于Int32.

public static object GetValue(Enum e)
{
    return e.GetType().GetField("value__").GetValue(e);
}

Debug.Assert(Equals(GetValue(DayOfWeek.Wednesday), 3));                //Int32
Debug.Assert(Equals(GetValue(AceFlags.InheritOnly), (byte) 8));        //Byte
Debug.Assert(Equals(GetValue(IOControlCode.ReceiveAll), 2550136833L)); //Int64

Note: I have only tested this with the Microsoft C# compiler. It's a shame there doesn't appear to be a built in way of doing this.

注意:我仅使用 Microsoft C# 编译器对此进行了测试。很遗憾,似乎没有内置的方式来做到这一点。

回答by Fernando Teles

This is the most simple way to solve your problem:

这是解决您问题的最简单方法:

public static void GetEnumMemberValue<T>(T enumItem) where T : struct 
{
    return (int) Enum.Parse(typeof(T), enumItem.ToString());
}

It works for me.

这个对我有用。