我看到了两者的建议,但想确认这是执行此操作的最佳方法:
public enum MO
{
Learn = 0,
Practice = 1,
Quiz = 2
}
public static partial class Extensions
{
public static MO ToMode(this string mode)
{
switch (mode)
{
case "Learn": return MO.Learn;
case "Practice": return MO.Practice;
case "Quiz": return MO.Quiz;
default: throw new InvalidEnumArgumentException("Unhandled value: " + mode.ToString());
}
}
}
Run Code Online (Sandbox Code Playgroud)