0

我正在阅读 txt 文件,我想将它分成几个部分。我的 TXT 文件的这个例子:

"Part error(1) devic[3].data_type(2)"
"Escape error(3) device[10].data_type(12)"

我想实现这样一种情况,当我有第一个单词“Part”时,我想为其设置枚举类型,并且在 switch 中我想调用一些适用于整行的函数,另一方面,当我有第一个单词“Escape”时,switch 中会有另一个 case 会调用其他函数。我该怎么做?到目前为止,这是我的代码:

class Row
{
    public enum Category { Part, Escape }
    public string Error{ get; set; }
    public string Data_Type { get; set; }
    public string Device{ get; set; }
}

public object HandleRegex(string items)
{
        Row sk = new Row();
        Regex r = new Regex(@"[.]");
        var newStr = r.Replace(items, @" ");

        switch(this.category)
        {
            case Category.Part:
                //I want to call here function HandlePart with my line as a parameter
                HandlePart(newStr);
                break;
            case Category.Escape:
                //Here I want to call Function HandleEscape for line with "Escape" word
                HandleEscape(newStr);
                break;

        }
}
4

4 回答 4

2
public object HandleRegex(string items)
{        
    Regex r = new Regex(@"[.]");
    var newStr = r.Replace(items, @" ");
    try {
     category = (Category) new EnumConverter(typeof(Category)).ConvertFromString(items.Split(new string[]{" "},StringSplitOptions.RemoveEmptyEntries)[0]);
        }
    catch {
       throw new ArgumentException("items doesn't contain valid prefix");
    }
    switch(category)
    {
        case Category.Part:
            HandlePart(newStr);
            break;
        case Category.Escape:
            HandleEscape(newStr);
            break;
    }
 }
于 2013-08-05T06:29:17.690 回答
1

您可以根据类别创建Dictionary<Category, Action<string>>并使用它来调用代码:

static void Main(string[] args)
{
    var input = @"Part error(1) devic[3].data_type(2)
Escape error(3) device[10].data_type(12)";

    var functions = new Dictionary<Category, Action<string>>()
    {
        { Category.Part, HandlePart},
        { Category.Escape, HandleEscape }
    };

    foreach (var line in input.Split(new [] {Environment.NewLine }, StringSplitOptions.None))
    {
        Category category;
        if(Enum.TryParse<Category>(line.Substring(0, line.IndexOf(' ')), out category) && functions.ContainsKey(category))
            functions[category](line);
    }
}

static void HandlePart(string line)
{
    Console.WriteLine("Part handler call");
}

static void HandleEscape(string line)
{
    Console.WriteLine("Escape handler call");
}

上述程序的输出:

Part handler call
Escape handler call
于 2013-08-05T06:30:54.763 回答
1

你可以使用TryParse

Category outCategory;
Enum.TryParse(this.category, out outCategory)

switch(outCategory)
        {
            case Category.Part:
                //I want to call here function HandlePart with my line as a parameter
                HandlePart(newStr);
                break;
            case Category.Escape:
                //Here I want to call Function HandleEscape for line with "Escape" word
                HandleEscape(newStr);
                break;
            default:
                // Needs to be handled
        }
于 2013-08-05T06:13:03.280 回答
0

如果你逐行阅读文件,那么你可以做

string str = file.ReadLine();
string firstWord = str.substring(0, str.IndexOf(' ')).Trim().ToLower();

现在你有了你可以做的第一句话

switch(firstWord){
  case "escape":// your code
  case "Part":// your code
}
于 2013-08-05T06:27:31.970 回答