English 中文(简体)
Enum and Dictionary <Enum, Action>
原标题:Enum and Dictionary<Enum, Action>

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

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

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".

在这里更清楚的是,我们又努力做到:

Enum:

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

界定我们的理论:

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 }
};

因此,基本上我所说的话是,是否有办法添加Reason, Function pair more intelligently? 因为正如你在理赔之后可以看到的那样,所有其他理由都使用同样的功能。

感谢你的建议。

最佳回答

你可以尝试像这样的东西,只把习俗理智手放在独裁者身上,然后回到将军身上。

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;
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签