如何在字典中存储来自 C# 中不同类的不同类型的方法

托马斯0910

我正在构建一个 Windows 应用程序,以便能够读取包含大量标记的 txt 文件,这些标记解释了所提供的信息类型。使用的标记看起来像“#FLAG xxx”,其中 xxx 是检索到的信息。根据使用的标记,信息的处理方式不同,因此我需要不同的方法来提取它。

在我的程序中,我使用字典来存储文件中可以包含的所有可能的标记变体,然后我读取文件中的每一行并比较字典中是否有命中。就像现在一样,我硬编码了要调用的方法,但觉得这一定是解决这个问题的愚蠢方法。我从另一个类(设置)加载 Dictionaryn 中包含的所有内容,这意味着我无法访问我放在第一个类(Form1)中的方法。

我知道我在这里犯了很多错误,但我对 C# 真的很陌生,我自己无法做更好的解决方案。如何更好的实践来解决这个问题。如果我不需要,我不想写很多 if 语句。

下面的代码是我所写的向下版本。否则读起来会很乱。是的,我删除了 Excel 导出部分,但希望您无论如何都能看到我正在尝试做的事情。

Forms1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace MarkupReader
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    OpenFileDialog ofd = new OpenFileDialog();

    private void button1_Click(object sender, EventArgs e)
    {
        ofd.Filter = "File (*.txt)|*.txt|All files (*.*)|*.*";

        ofd.Title = "Open file";
        ofd.Multiselect = false;

        if (ofd.ShowDialog() == DialogResult.OK)
        {

            StreamReader readFile = new StreamReader(File.OpenRead(ofd.FileName), Encoding.GetEncoding(437));
            ExportFile(readFile);
            readFile.Dispose();
        }
    }

    private void ExportFile(StreamReader readFile)
    {
        Dictionary<string, Tuple<string, int, string>> markup = new Dictionary<string, Tuple<string, int, string>>();

        readFile.DiscardBufferedData();
        readFile.BaseStream.Seek(0, System.IO.SeekOrigin.Begin);

        Settings settings = new Settings();
        settings.SetMarkup(markup);

        string fileMarkup = "";
        string fileMarkupValues = "";
        string line = "";

        while (line != null)
        {
            line = readFile.ReadLine();
            if (line != null)
            {
                //Get markup
                fileMarkup = line.Trim().Split(' ').First();
                fileMarkupValues = line.Replace(fileMarkup, "");

                if (markup.ContainsKey(fileMarkup))
                {
                    /* markupItemX = X is content
                     * Item1 = User flag
                     * Item2 = Export to worksheet number
                     * Item3 = Extract method to call
                     * Item4 = 
                     */

                    //Export to excel sheet 1
                    if (markup[fileMarkup].Item2 == 1)
                    {
                        if (markup[fileMarkup].Item3 == "extractDataOne") ExtractMethodOne(fileMarkupValues);
                        if (markup[fileMarkup].Item3 == "extractDataTwo") ExtractMethodTwo(fileMarkupValues);
                        if (markup[fileMarkup].Item3 == "extractDataThree") ExtractMethodThree(fileMarkupValues);
                        if (markup[fileMarkup].Item3 == "extractDataFour") ExtractMethodFour(fileMarkupValues);
                        if (markup[fileMarkup].Item3 == "extractDataFive") ExtractMethodFive(fileMarkupValues);
                    }
                    //Export to excel sheet 2
                    if (markup[fileMarkup].Item2 == 2)
                    {
                        if (markup[fileMarkup].Item3 == "extractDataOne") ExtractMethodOne(fileMarkupValues);
                        if (markup[fileMarkup].Item3 == "extractDataTwo") ExtractMethodTwo(fileMarkupValues);
                        if (markup[fileMarkup].Item3 == "extractDataThree") ExtractMethodThree(fileMarkupValues);
                        if (markup[fileMarkup].Item3 == "extractDataFour") ExtractMethodFour(fileMarkupValues);
                        if (markup[fileMarkup].Item3 == "extractDataFive") ExtractMethodFive(fileMarkupValues);
                    }
                }
            }
        }
    }

    public void ExtractMethodOne(string markupData)
    {
        //Extract stuff and write to Excel
    }

    public void ExtractMethodTwo(string markupData)
    {
        //Extract stuff and write to Excel
    }

    public void ExtractMethodThree(string markupData)
    {
        //Extract stuff and write to Excel
    }

    public void ExtractMethodFour(string markupData)
    {
        //Extract stuff and write to Excel
    }

    public void ExtractMethodFive(string markupData)
    {
        //Extract stuff and write to Excel
    }

}
}

Setting.cs(它确实包含了很多,这只是一个例子)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarkupReader
{
class Settings
{
    public void SetMarkup(Dictionary<string, Tuple<string, int, string>> markup)
    {
        int workSheet = 1;
        markup.Add("#FLAG", new Tuple<string, int, string>("User flag", workSheet, "extractDataOne"));
        markup.Add("#FORMAT", new Tuple<string, int, string>("User format", workSheet, "extractDataOne"));
        markup.Add("#TYPE", new Tuple<string, int, string>("User type", workSheet, "extractDataOne"));
    }
}
}
海塔姆

根据您的ExtractX方法的签名(带有一个字符串参数的 void),您可以执行以下操作:

// Static field or whatever you want
Dictionary<string, Action<string>> methods = new Dictionary<string, Action<string>>()
{
    { "extractDataOne", ExtractMethodOne },
    { "extractDataTwo", ExtractMethodTwo },
    { "extractDataThree", ExtractMethodThree },
    { "extractDataFour", ExtractMethodFour },
    { "extractDataFive", ExtractMethodFive },
};

// And then use it like this
methods[markup[fileMarkup].Item3].Invoke(fileMarkupValues);

尽管我不明白您将如何区分工作表 1 和工作表 2,因为您对这两种情况都使用相同的方法。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

与c中不同类型的对象堆叠

来自分类Dev

在C中添加不同类型的变量

来自分类Dev

在C#中将不同类型存储在集合中

来自分类Dev

如何从C ++中的switch语句返回不同类型的变量

来自分类Dev

c中零长度位字段的不同类型?

来自分类Dev

C#中不同类型的实例化之间的区别

来自分类Dev

C中具有不同类型的外部变量

来自分类Dev

在C中声明外部变量的不同类型

来自分类Dev

C#(清单系统)中不同类型的列表

来自分类Dev

具有返回不同类型子项的抽象类中的C#抽象方法

来自分类Dev

收集不同类型的类并在C ++中调用其方法

来自分类Dev

C ++中不同类文件中的主要方法?

来自分类Dev

C ++中不同类文件中的主要方法?

来自分类Dev

OOP中不同类型的方法

来自分类Dev

如何在C#中的单个对象数组中访问不同类型的类

来自分类Dev

不同类 C# 中的多个 TransactionScope

来自分类Dev

如何在C ++中处理具有多个不同类型的数据成员的类?

来自分类Dev

如何在C中传递N个不同类型的参数

来自分类Dev

如何在C中对每列都是不同类型的二维数组进行排序?

来自分类Dev

如何在现代C ++中将不同类类型的对象存储到一个容器中?

来自分类Dev

如何正确地将int类型的算术运算结果存储为C ++中的double类型和不同类型?

来自分类Dev

根据c#中的类型将不同类型的json列表分组

来自分类Dev

根据c#中的类型将不同类型的json列表分组

来自分类Dev

C++中根据模板类型返回不同类型的对象

来自分类Dev

如何定义与每个维度的不同类型的C ++中的3D阵列?

来自分类Dev

如何减少C#中不同类型的相似函数的重复代码

来自分类Dev

在C ++中,如何使构造函数具有不同类型的参数?

来自分类Dev

在 C# 中,如何使用具有两种不同类型的泛型?

来自分类Dev

如何让我的 CSV 文件阅读器读取 C++ 中的不同类型?

Related 相关文章

  1. 1

    与c中不同类型的对象堆叠

  2. 2

    在C中添加不同类型的变量

  3. 3

    在C#中将不同类型存储在集合中

  4. 4

    如何从C ++中的switch语句返回不同类型的变量

  5. 5

    c中零长度位字段的不同类型?

  6. 6

    C#中不同类型的实例化之间的区别

  7. 7

    C中具有不同类型的外部变量

  8. 8

    在C中声明外部变量的不同类型

  9. 9

    C#(清单系统)中不同类型的列表

  10. 10

    具有返回不同类型子项的抽象类中的C#抽象方法

  11. 11

    收集不同类型的类并在C ++中调用其方法

  12. 12

    C ++中不同类文件中的主要方法?

  13. 13

    C ++中不同类文件中的主要方法?

  14. 14

    OOP中不同类型的方法

  15. 15

    如何在C#中的单个对象数组中访问不同类型的类

  16. 16

    不同类 C# 中的多个 TransactionScope

  17. 17

    如何在C ++中处理具有多个不同类型的数据成员的类?

  18. 18

    如何在C中传递N个不同类型的参数

  19. 19

    如何在C中对每列都是不同类型的二维数组进行排序?

  20. 20

    如何在现代C ++中将不同类类型的对象存储到一个容器中?

  21. 21

    如何正确地将int类型的算术运算结果存储为C ++中的double类型和不同类型?

  22. 22

    根据c#中的类型将不同类型的json列表分组

  23. 23

    根据c#中的类型将不同类型的json列表分组

  24. 24

    C++中根据模板类型返回不同类型的对象

  25. 25

    如何定义与每个维度的不同类型的C ++中的3D阵列?

  26. 26

    如何减少C#中不同类型的相似函数的重复代码

  27. 27

    在C ++中,如何使构造函数具有不同类型的参数?

  28. 28

    在 C# 中,如何使用具有两种不同类型的泛型?

  29. 29

    如何让我的 CSV 文件阅读器读取 C++ 中的不同类型?

热门标签

归档