c# - 有没有办法重构 C# 独有的 switch 语句?

标签 c# c++ switch-statement refactoring

我是 C# (.NET 4.0) 项目的新员工,但我有 C/C++ 背景。我被指示“全面改进我们的产品”,并遇到了一个甚至 1000 行长的方法,它主要由一个包含 15 个案例的 switch 语句组成,每个案例大约有 60 行代码。

在这次演出之前,我会保留相同的通用格式,但会提取 15 个函数,然后研究如何根据需要共享/指向局部变量/类数据成员。但是,我现在可以使用大量神秘的新工具,而且我无法摆脱一种“C# 智能”方式来解决这个问题的感觉。

有没有办法在 C#(4.0) 中重构这个在 C++ 中无法完成的 switch 语句?

编辑:这是经过清理和总结的代码形式:

private void DeviceRequest(List<OpRequest> currentOp, double adjTime) {
     //local variables

    while (/*condition*/)
    {
        //Some setup
        //Some initialization

        try
        {
            switch (OperationType)
            {
                case Setup:
                    //Message header setup through 'if-else' statements
                    //Message sending
                    //Many nested 'if' checks, a few less 'else's
                    break;

                case Initiate:
                    //Message header setup through 'if-else' statements
                    //Message sending
                    //Many nested 'if' checks, a few less 'else's
                    break;

                case Acquire:
                    //Message header setup through 'if-else' statements
                    //Message sending
                    //Many nested 'if' checks, a few less 'else's
                    break;

                case Measure:
                    //Message header setup through 'if-else' statements
                    //Message sending
                    //Many nested 'if' checks, a few less 'else's
                    break;

                case Reset:
                    //Message header setup through 'if-else' statements
                    //Message sending
                    //Many nested 'if' checks, a few less 'else's
                    break;

                //12 more cases, all fairly similar.
            }
        }

        catch (RunTimeError err)
        {
            //A few lines of code
        }

        finally
        {
          //Objective measure of time passage
        }
    }
}

最佳答案

A switchenum 上通常可以用 Dictionary 代替行动。为了为此做好准备,每个 Action 都必须处理相同的输入。

如果您可以将代码重构为 15 个具有相同签名的方法的集合,则可以将它们放入 Dictionary<OpRequest,DelegateType> 中其中 DelegateType是一个带有您的方法签名的委托(delegate)。

例如,如果这 15 个方法中的每一个都具有以下签名

private void SetupAction(double adjTime) {
    ...
}
void InitiateAction(double adjTime) {
    ...
}

你可以建立一个 Action 字典

private readonly IDictionary<OpRequest,Action<double>> OpActions = new Dictionary<OpRequest,Action<double>> {
    {OperationType.Setup, SetupAction}
,   {OperationType.Initiate, InitiateAction}
,   ... // and so on
};

有了这个字典,您可以按如下方式重写循环:

while (/*condition*/) {
    //Some setup
    //Some initialization
    try {
        Action<double> action;
        if (OpActions.TryGetValue(opType, out action)) {
            action(adjTime);
        } else {
            ... // Report an error: unknown OpRequest
        }
    } catch (RunTimeError err) {
        ... //A few lines of code
    }
    finally {
        ... //Objective measure of time passage
    }
}

关于c# - 有没有办法重构 C# 独有的 switch 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34399712/

相关文章:

java - android java switch 语句中缺少或删除循环周期

PHP/MYSQL : How to order a list of items clicking a link

c# - 如何动态设置哪些属性绑定(bind)到 DataGridView?

android - 无法使用套接字显示接收到的缓冲区

c++ - 科学编程实践

c++ - Qt 解决方案 QtPropertyBrowser : member access into incomplete type

c# - 使用枚举和 switch 语句 c#

c# - DataGridView 缺少数据源为空的新行

c# - ReadOnly=true 和 TextBox1.Attributes.Add ("readonly", "readonly") 在 ASP.NET 中有什么区别?

c# - System.Double[*] 是什么意思