使用“开关”;与资源文件中的字符串

发布于 2024-10-19 02:03:57 字数 1053 浏览 4 评论 0原文

我的资源(.resx)文件中有一堆字符串。我试图直接将它们用作 switch 语句的一部分(请参阅下面的示例代码)。

class Test
{
    static void main(string[] args)
    {
        string case = args[1];
        switch(case)
        {
            case StringResources.CFG_PARAM1: // Do Something1 
                break;
            case StringResources.CFG_PARAM2: // Do Something2
                break;
            case StringResources.CFG_PARAM3: // Do Something3
                break;              
            default:
                break;
        }
    }
}

我查看了一些解决方案,其中大多数似乎建议我需要将它们声明为我个人不喜欢的 const string 。 我喜欢这个问题的投票最高的解决方案: 在 switch 语句中使用字符串集合。但随后我需要确保资源文件中的 enumstring 绑定在一起。我想知道一种巧妙的方法。

编辑: 在研究如何使用时还发现这个很好的答案 >操作

I have a bunch of strings in my resource(.resx) file. I am trying to directly use them as part of switch statement (see the sample code below).

class Test
{
    static void main(string[] args)
    {
        string case = args[1];
        switch(case)
        {
            case StringResources.CFG_PARAM1: // Do Something1 
                break;
            case StringResources.CFG_PARAM2: // Do Something2
                break;
            case StringResources.CFG_PARAM3: // Do Something3
                break;              
            default:
                break;
        }
    }
}

I looked at some of the solutions, most of them seem to suggest that I need to declare them as const string which I personally dislike.
I liked the top voted solution for this question: using collection of strings in a switch statement. But then I need to make sure that my enum and strings in resource file are tied together. I would like to know a neat way of doing that.

Edit:
Also found this great answer while researching how to use Action:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

记忆里有你的影子 2024-10-26 02:03:57

您可以使用Dictionary。您为字典中的每个字符串放置一个Action(方法的委托)并搜索它。

var actions = new Dictionary<string, Action> {
    { "String1", () => Method1() },
    { "String2", () => Method2() },
    { "String3", () => Method3() },
};

Action action;

if (actions.TryGetValue(myString, out action))
{
    action();
}
else
{
    // no action found
}

作为旁注,如果 Method1 已经是 Actionvoid Method1() 方法(没有参数,没有返回值),您可以做

    { "String1", (Action)Method1 },

You could use a Dictionary<string, Action>. You put an Action (a delegate to a method) for each string in the Dictionary and search it.

var actions = new Dictionary<string, Action> {
    { "String1", () => Method1() },
    { "String2", () => Method2() },
    { "String3", () => Method3() },
};

Action action;

if (actions.TryGetValue(myString, out action))
{
    action();
}
else
{
    // no action found
}

As a sidenote, if Method1 is already an Action or a void Method1() method (with no parameters and no return value), you could do

    { "String1", (Action)Method1 },
ペ泪落弦音 2024-10-26 02:03:57

你不能那样做。编译器必须能够计算这些值,这意味着它们必须是文字或常量。

You can't do that. The compiler must be able to evaluate the values, which means that they need to be literals or constants.

飘逸的'云 2024-10-26 02:03:57

我自己刚刚遇到这个问题,虽然这篇文章很旧,但我想我应该为其他“Google 员工”分享我的简单解决方案...我选择将 switch...case 更改为多个 if(...) elseif

class Test
{
    static void main(string[] args)
    {
        string case = args[1];
        if(case.Equals(StringResources.CFG_PARAM1))
        {
            // Do Something1
        }
        else if (case.Equals(StringResources.CFG_PARAM2))
        {
            // Do Something2
        }
        else if (case.Equals(StringResources.CFG_PARAM3))
        {
            // Do Something3
        }
        else
        {
            // Do something else
        }
    }
}

绝对不如 switch...case 漂亮,但对我有用。

I just came across this problem myself, and although this post is old, I thought I'd share my simple solution for other "Googlers"... I opted to change the switch...case to multiple if(...) elseif

class Test
{
    static void main(string[] args)
    {
        string case = args[1];
        if(case.Equals(StringResources.CFG_PARAM1))
        {
            // Do Something1
        }
        else if (case.Equals(StringResources.CFG_PARAM2))
        {
            // Do Something2
        }
        else if (case.Equals(StringResources.CFG_PARAM3))
        {
            // Do Something3
        }
        else
        {
            // Do something else
        }
    }
}

Definitely not as pretty as switch...case but has worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文