我怎么能一般地演绎枚举?

Ste*_*hen 2 c# enums parsing

我正在尝试解决一个简单的解析问题,我选择使用枚举来编码选择列表.

输入数据是直接的ascii文本,分为具有唯一标头的块和数据所在的非唯一标识符.我能够编写非常通用的符号化方法,而不提供有关数据含义的任何上下文,并在返回后处理它.

用字符串执行此操作是没有问题的.我只是通过一个List而去.

我无法弄清楚枚举枚举的语法,我可以使用一些帮助.我也可能太过于陷入命令式思考,并且错过了一个简单的答案.

这是我遇到困难的代码

private void parseToEnums(Enum returnEnum, string searchBlock, string startIDText,
                          string endIDText, string startText, string endText)
{
    string ourSearchBlock = searchBlock;
    int endIDidx = ourSearchBlock.IndexOf(endIDText);

    while (ourSearchBlock.IndexOf(startText) != -1)
    {
        if (ourSearchBlock.Length == searchBlock.Length)
        {
            // first pass, trim off the region where the start text isn't valid
            ourSearchBlock = ourSearchBlock.Remove(endIDidx, ourSearchBlock.Length - endIDidx);
            // first pass, use the startIDtext to create a valid search zone
            // BROKEN CODE HERE
            // Neither GetType() nor typeof seem to do the right thing
            // I have tried several varieties and have tried casting the LHS in the
            // same sort of way
            // pluckText returns a string that is guaranteed to match an enum name
            returnEnum = (returnEnum.GetType()) System.Enum.Parse(typeof(returnEnum), pluckText(ourSearchBlock, startIDText, startText, endText), false);
            ourSearchBlock = ourSearchBlock.Remove(0, ourSearchBlock.IndexOf(startIDText) + startIDText.Length);
        }
        else
        {
            // this would be similar to the above after it's working
            // and is for the case where the string has multiple matches
            // within the enum, ie "red white"
            //returnList.Add(pluckText(ourSearchBlock, "", startText, endText));
        }
        ourSearchBlock = ourSearchBlock.Remove(0, ourSearchBlock.IndexOf(startText) + startText.Length);
    }

    return;
}
Run Code Online (Sandbox Code Playgroud)

我在做什么的例子

private enum Colors { red, white, green };
private enum Suits  { spades, clubs, hearts, diamonds };

// ... open files, read data, etc
// so I pass in the enum that I want my result in and some text identifiers

parseToEnum ( Colors, searchBlock, "startColorBlock", "endColorBlock", "id=" );
parseToEnum ( Suits, searchBlock, "startCardSuitsBlock", "endCardSuitsBlock", "<id=" );

// ...
Run Code Online (Sandbox Code Playgroud)

所以我的想法是使用相同的结构(因为输入是相同的),但使用不同的枚举输出.

我知道我需要在太长时间之前将一些try/catch包装器和一般错误检测添加到此代码中.

svi*_*ick 9

我会忽略所有的搜索和重点转换stringenum.

首先,我认为你的方法应该返回结果,而不是将它作为参数传递(你需要out它).

其次,要将枚举的类型传递给方法,您可以使用类型的参数Type,或者更好的是,使方法通用并将类型作为类型参数传递.

该方法可能如下所示:

T ParseEnum<T>(string s)
{
    return (T)Enum.Parse(typeof(T), s, false);
}
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样调用它:

Colors color = ParseEnum<Colors>(someString);
Run Code Online (Sandbox Code Playgroud)

您的代码中的错误是:

  • Enum是所有enums 的常见基类型,它不代表一个类型enum.这意味着您不能使用例如Colors作为方法的参数.
  • 您不能转换为仅在运行时已知的类型.换句话说,代码(foo.GetType())bar就永远不会起作用.
  • 您不能使用typeof运算符来获取变量的类型.您可以使用它来获取Type某些特定类型的对象,例如,typeof(string)或者typeof(T)在带有类型参数的泛型方法中T.
  • 类型(包括enums)的名称应该是单数.那是因为例如类型的变量Color代表一种颜色.虽然这只是一个样式问题,但它不会阻止您的代码工作.但它会让你的代码更难理解.