Refactor long switch statement

前端 未结 6 1412
我寻月下人不归
我寻月下人不归 2020-12-06 06:09

I\'m program in c# which you controlling by dictating command so now i have a long switch statement. Something like

switch (command)

{
    case \"Show comma         


        
6条回答
  •  时光取名叫无心
    2020-12-06 06:44

    If all the functions get the same parameters and return the same value, you can use a Dictionary along with delegates to map a string to a function(s). This method will allow you also to change in run time the switch - allowing external programs to extend the functionality of the program.

    If the functions aren't the same, you could write wrappers - a proxy function that will get parameters as all other functions, and call the functions you want.

提交回复
热议问题