c# 7.0: switch on System.Type

后端 未结 9 1994
既然无缘
既然无缘 2020-12-03 04:10

No existing question has an answer to this question.

In c# 7, can I switch directly on a System.Type?

When I try:

    switch (Type)         


        
9条回答
  •  一生所求
    2020-12-03 04:42

    Here is an alternative which won't compile as the classes are missing:

    bool? runOnUI = queuedAction.RunOnUI;  // N=null, T=true F=False
    bool isOnUI = Statics.CoreDispatcher.HasThreadAccess;
    bool isFF = queuedAction.IsFireAndForget;   // just makes it easier to read the switch statement
    if (false == queuedAction.IsFireAndForget)
        IsOtherTaskRunning = true;
    
    /* In the case statements below the string name is something like noFF_TN
     * The compiler ignores the string name. I have used them here to represent
     * the logic in the case statement:   ( FF = FireAnd Forget, T=true, F=false, N = null, * means 'whatever')
     * 
     *      isFF_** = FireAndForget QueuedAction
     *      noFF_** = Not FireAndForget so Wait for Task to Finish (as opposed to Complete)
     *      ****_T* = We are already on the UI thread 
     *      ****_F* = We are not on the UI thread 
     *      ****_*T = Run on the UI thread.
     *      ****_*F = Do not run on UI thread
     *      ****_*N = Don't care so run on current thread
     *      
     *  The last character is a "bool?" representing RunOnUI. It has
     *  three values each of which has a different meaning */
    
    bool isTask;
    switch ("ignore")
    {
        /* Run it as an Action (not Task) on current Thread   */
        case string noFF_TT when !isFF && isOnUI && runOnUI == true:
        case string isFF_TN when isFF && isOnUI && !runOnUI == null:
        case string isFF_FN when isFF && !isOnUI && runOnUI == null:
        case string isFF_TT when isFF && isOnUI && runOnUI == true:
        case string isFF_FF when isFF && !isOnUI && runOnUI == false:
            isTask = false;
            queuedAction.ActionQA(queuedAction); // run as an action, not as a Task
            break;
    
        /* Create a Task, Start it */
    
        case string isFF_TF when isFF && isOnUI == true && runOnUI == false:
        case string noFF_TN when !isFF && isOnUI == true && runOnUI == null:     // <== not sure if I should run it on UI instead
        case string noFF_TF when !isFF && isOnUI && runOnUI == false:
        case string noFF_FN when !isFF && !isOnUI && runOnUI == null:
        case string noFF_FF when !isFF && !isOnUI && runOnUI == false:
            var cancellationTokenSource = new CancellationTokenSource();
    queuedAction.Canceller?.SetCancellationTokenSource(cancellationTokenSource);
            isTask = true;
            new Task
                (
                    (action) => queuedAction.ActionQA(queuedAction),
                    queuedAction,
                    cancellationTokenSource.Token
                )
                .Start();
            break;
    
        /* Run on UI and don't wait */
    
        case string isFF_FT when isFF && !isOnUI && runOnUI == true:
            isTask = true;
            Statics.RunOnUI(() => queuedAction.ActionQA(queuedAction), asTaskAlways: true);
            break;
    
        /* Run on the UI as a Task and Wait */
    
        case string noFF_FT when !isFF && !isOnUI && runOnUI == true:
            isTask = true;
            Statics.RunOnUI(() => queuedAction.ActionQA(queuedAction), asTaskAlways: true);
            break;
    
        default:
            throw new LogicException("unknown case");
    }
    

提交回复
热议问题