Use variable in place of function name

后端 未结 6 1575
自闭症患者
自闭症患者 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:40

    I believe what you're looking for will need some refactoring of your code.

    All of your "cases" (TextFile, MSOfficeWordFile, MSOfficeExcelFile, PdfFile) should be their own classes which implement a single interface.

    Your interface should be named something like "IReadableFile" and specify a method named "ReadFile()".

    Each class should have their own implementation of "ReadFile()"

    Example:

    public interface IReadableFile
    {
        void ReadFile();
    }
    

    public class MSOfficeWordFile : IReadableFile
    {
        public void ReadFile()
        {
            ReadMSOfficeWordFile(file);
        }
    } 
    

    foreach(IReadableFile file in files)
        file.ReadFile();
    

    Code may contain some mistakes, but I hope you get the idea.

提交回复
热议问题