有没有办法重构一个C#独有的switch语句?

时间:2015-12-21 16:00:22

标签: c# c++ switch-statement refactoring

我是C#(.NET 4.0)项目的新雇员,但我来自C / C ++背景。我被指示“通常改进我们的产品”,并且遇到了一个甚至1000行长的方法,该方法主要由单个switch语句组成,包含15个案例,每个代码大约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
        }
    }
}

1 个答案:

答案 0 :(得分:1)

switch上的enum通常可以由Dictionary个动作取代。为了做好准备,每个动作必须处理相同的输入。

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

例如,如果15种方法中的每一种都具有下面的签名

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

你可以建立行动词典

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