Use variable in place of function name

后端 未结 6 1585
自闭症患者
自闭症患者 2021-02-09 13:16

I\'m using the following right now:

foreach (string file in files) {
    switch (filetype.Value) {
        case \"ReadFile\":
            ReadFile(file);
                


        
6条回答
  •  情话喂你
    2021-02-09 13:32

    You can do it with some preparation using delegates, like this:

    private static readonly IDictionary> actionByType =
        new Dictionary> {
            {"ReadFile", ReadFile}
        ,   {"ReadMSOfficeWordFile", ReadMSOfficeWordFile}
        ,   {"ReadMSOfficeExcelFile", ReadMSOfficeExcelFile}
        ,   {"ReadPDFFile", ReadPDFFile}
        };
    

    When it is time to call your action, do it as follows:

    actionByType[actionName](file);
    

提交回复
热议问题