C# 如何常规强制转换枚举?
我试图解决一个简单的解析问题,我选择使用枚举对选择列表进行编码 输入数据是直接的ascii文本,分为具有唯一标题的块和数据所在位置的非唯一标识符。我能够编写非常通用的符号化方法,而无需提供任何关于数据含义的上下文,并在返回数据后进行处理 使用字符串执行此操作没有问题。我只是递了张单子,然后我们就走了 我想不出概括枚举的语法,我需要一些帮助。我也可能过于拘泥于命令式思维,而错过了一个简单的答案 这是我很难理解的代码C# 如何常规强制转换枚举?,c#,parsing,enums,C#,Parsing,Enums,我试图解决一个简单的解析问题,我选择使用枚举对选择列表进行编码 输入数据是直接的ascii文本,分为具有唯一标题的块和数据所在位置的非唯一标识符。我能够编写非常通用的符号化方法,而无需提供任何关于数据含义的上下文,并在返回数据后进行处理 使用字符串执行此操作没有问题。我只是递了张单子,然后我们就走了 我想不出概括枚举的语法,我需要一些帮助。我也可能过于拘泥于命令式思维,而错过了一个简单的答案 这是我很难理解的代码 private void parseToEnums(Enum returnEnum
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;
}
我正在做的例子
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=" );
// ...
私有枚举颜色{红、白、绿};
私人enum套装{黑桃、梅花、红桃、钻石};
// ... 打开文件、读取数据等
//所以我传入了我想要结果的枚举和一些文本标识符
parseToEnum(颜色、搜索块、“startColorBlock”、“endColorBlock”、“id=”);
parseToEnum(suites,searchBlock,“startCardSuitesBlock”,“endCardSuitesBlock”,“我将忽略所有搜索,并将重点放在将字符串
转换为枚举上
首先,我认为您的方法应该返回结果,而不是将其作为参数传递(您需要out
)
其次,要将枚举的类型传递给该方法,可以使用类型为type
的参数,或者更好的方法是使该方法泛型并将该类型作为类型参数传递
该方法可以如下所示:
T ParseEnum<T>(string s)
{
return (T)Enum.Parse(typeof(T), s, false);
}
Colors color = ParseEnum<Colors>(someString);
T ParseEnum(字符串s)
{
返回(T)Enum.Parse(typeof(T),s,false);
}
你可以这样称呼它:
T ParseEnum<T>(string s)
{
return (T)Enum.Parse(typeof(T), s, false);
}
Colors color = ParseEnum<Colors>(someString);
Colors color=ParseEnum(someString);
代码中的错误有:
Enum
是所有Enum
的公共基类型,它不表示Enum
的类型。这意味着您不能将例如颜色
用作方法的参数
- 您不能强制转换为只有在运行时才知道的类型。换句话说,像
(foo.GetType())bar
这样的代码永远不会工作
- 不能使用
typeof
运算符来获取变量的类型。可以使用它来获取某些特定类型的type
对象,例如带有类型参数t
的泛型方法中的typeof(string)
或typeof(t)
- 类型(包括
enum
s)的名称应为单数。这是因为例如Color
类型的变量代表一种颜色。虽然这只是一个样式问题,不会阻止代码工作。但它会使代码更难理解
可以忽略搜索:-)如果我没有把它包含进来,有人会因为没有提供足够的细节而对我大发雷霆。我很喜欢它,会让它旋转。我也喜欢用这个方法来写颜色和……。你写了这个,但是它可能不会按你想要的方式工作。如果你想要一个<代码> EnUM <代码>,它可以代表同一个TI上的几个值。例如,您必须将值设置为非重叠二进制值(例如1、2、4、8等)然后使用|
组合它们。你提出了一个伟大的观点,在进行了大量无意义的调试之后,我会想到这个观点……很久以前我就知道了。我确实希望枚举的工作方式更像位集,你是对的,我需要将其视为用于设置和获取的。