如何从枚举中获取数值?

时间:2022-05-01 20:53:55

For example System.Net.HttpStatusCode Enum, I would like to get the HTTP Status Codes instead of the HTTP Status Text. System.Net.HttpStatusCode.Forbidden should return 403 instead of "Forbidden".

例如System.Net.HttpStatusCode Enum,我想获取HTTP状态代码而不是HTTP状态文本。 System.Net.HttpStatusCode.Forbidden应返回403而不是“Forbidden”。

How can I extract the value?

我该如何提取价值?

3 个解决方案

#1


74  

For the majority of Enum's simply cast to the base type which is int32.

对于大多数Enum来说,只需要转换为int32的基类型。

int value = (int)System.Net.HttpStatusCode.Forbidden;

#2


17  

You can just cast it to an integer!

你可以把它变成一个整数!

int code = (int)enumVariable

#3


7  

System.Convert.ToInt32(response.StatusCode) returns the statusCode number

System.Convert.ToInt32(response.StatusCode)返回statusCode编号

#1


74  

For the majority of Enum's simply cast to the base type which is int32.

对于大多数Enum来说,只需要转换为int32的基类型。

int value = (int)System.Net.HttpStatusCode.Forbidden;

#2


17  

You can just cast it to an integer!

你可以把它变成一个整数!

int code = (int)enumVariable

#3


7  

System.Convert.ToInt32(response.StatusCode) returns the statusCode number

System.Convert.ToInt32(response.StatusCode)返回statusCode编号