在枚举中循环查找匹配值的最佳方法是什么?
string match = "A";
enum Sample { A, B, C, D }
foreach(...) {
//should return Sample.A
}发布于 2010-05-04 04:55:07
你在找Enum.Parse
Sample e = (Sample)Enum.Parse(typeof(Sample), match);您可以通过调用Enum.GetValues或Enum.GetNames遍历这些值。
发布于 2010-05-04 04:55:02
public Sample matchStringToSample(string match)
{
return (Sample)Enum.Parse(typeof(Sample), match);
}您必须处理字符串匹配不是有效枚举值的情况。在这种情况下,Enum.Parse会抛出ArgumentException。
发布于 2010-05-04 04:55:14
Enum.Parse(typeof(Sample), "A");https://stackoverflow.com/questions/2761105
复制相似问题