ENUM和字​​典<枚举,作用> [英] Enum and Dictionary<Enum, Action>

查看:96
本文介绍了ENUM和字​​典<枚举,作用>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我能的方式,很明显大家解释我的问题。 我们需要对这个您的建议。

I hope I can explain my problem in a way that it's clear for everyone. We need your suggestions on this.

我们有一个枚举类型,定义了超过15个常量。 我们收到来自Web服务的报告,并翻译了一列到这个枚举类型。 而根据我们从该Web服务接收,我们需要运行特定的功能 词典

We have an Enum Type which has more than 15 constants defined. We receive a report from a web service and translate its one column into this Enum type. And based on what we receive from that web service, we run specific functions using Dictionary

为什么我问的想法? 比方说,其中3枚举contants的满足特定功能在我们的词典,但其余使用相同的功能。那么,有没有办法将它们添加到我们的字典以更好的方式,而不是将它们一一?我也想保持这种结构,因为当它的时候,我们可能有特定的功能,在未来,我形容为休息。

Why am I asking for ideas? Let's say 3 of these Enum contants meet specific functions in our Dictionary but the rest use the same function. So, is there a way to add them into our Dictionary in a better way rather than adding them one by one? I also want to keep this structure because when it's time, we might have specific functions in the future for the ones that I described as "the rest".

要比较清楚这里有一个例子,我们正在努力做的事情:

To be more clear here's an example what we're trying to do:

枚举:

public enum Reason{
    ReasonA,
    ReasonB,
    ReasonC,
    ReasonD,
    ReasonE,
    ReasonF,
    ReasonG,
    ReasonH,
    ReasonI,
    ReasonJ,
    ReasonK
}

定义我们的字典:

Defining our Dictionary:

public Dictionary<Reason, Action<CustomClassObj, string>> ReasonHandlers = new Dictionary<Reason, Action<CustomClassObj, string>>{
    { Reason.ReasonA, HandleReasonA },
    { Reason.ReasonB, HandleReasonB },
    { Reason.ReasonC, HandleReasonC },
    { Reason.ReasonD, HandleReasonGeneral },
    { Reason.ReasonE, HandleReasonGeneral },
    { Reason.ReasonF, HandleReasonGeneral },
    { Reason.ReasonG, HandleReasonGeneral },
    { Reason.ReasonH, HandleReasonGeneral },
    { Reason.ReasonI, HandleReasonGeneral },
    { Reason.ReasonJ, HandleReasonGeneral },
    { Reason.ReasonK, HandleReasonGeneral }
};

因此​​,基本上是:有我在问什么方式来增加原因,功能对更智能?因为你可以ReasonC后看到,所有其他原因使用相同的功能。

So basically what I'm asking is, is there a way to add Reason, Function pair more intelligently? Because as you can see after ReasonC, all other reasons use the same function.

感谢您的建议。

推荐答案

您可以尝试这样的事情,只能把自定义的原因处理器在字典中,然后退回到常规的。

You could try something like this, only put the custom Reason handler in the dictionary and then fallback to the General one.

public Dictionary<Reason, Action<CustomClassObj, string>> ReasonHandlers = new Dictionary<Reason, Action<CustomClassObj, string>>{
    { Reason.ReasonA, HandleReasonA },
    { Reason.ReasonB, HandleReasonB },
    { Reason.ReasonC, HandleReasonC }};


public Action<CustomClassObj, string> ReasonHandlerLookup (Reason reason) {
    Action<CustomClassObj, string> result = null;
    ReasonHandlers.TryGetValue(reason, out result);
    return result ?? HandleReasonGeneral;
}

这篇关于ENUM和字​​典&LT;枚举,作用&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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