通过字符串获取枚举int值 [英] Get enum int value by string

查看:183
本文介绍了通过字符串获取枚举int值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了下面的枚举:

I have implemented the enum below:

public enum CaseOriginCode
    {
        Web = 0,
        Email = 1,
        Telefoon = 2
    }

想要通过字符串获取枚举int值。这样的东西:

I would like to get the enum int value by a string. Something like this:

public void setCaseOriginCode(string caseOriginCodeParam)
{
    int caseOriginCode = CaseOriginCode.GetEnumByString(caseOriginCodeParam);
    // do something with this integer
}

所以我的输入是字符串和我的输出需要是一个int。

So my input is a string and my output needs to be an int.

或者更好地实现具有键值的字典。密钥将是字符串,将是一个int。

Or is it better to implement an dictionary with an key value. The key will be the string and the will be an int.

推荐答案

请尝试使用以下代码片段。

Please try with the below code snippet.

public void setCaseOriginCode(string CaseOriginCode)
{
    int caseOriginCode = (int)(CaseOriginCode)Enum.Parse(typeof(CaseOriginCode), CaseOriginCode);
}

让我知道如果有任何疑虑。

Let me know if any concern.

这篇关于通过字符串获取枚举int值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆