C# 枚举和字符串互转
时间:2011-02-21 来源:Zcold
public static class EnumConvert<T> { /// <summary> Parse string to enum type /// </summary> /// <param name="s"> Input string </param> /// <returns> Parsing result enum object </returns> public static T ParseFromString(string s) { return (T)Enum.Parse(typeof(T), s); } }
使用方法
public enum Fruit { Orange, Apple, Grape } class Program { static void Main(string[] args) { // enum to string string s = Fruit.Grape.ToString(); // string to enum Fruit f = EnumConvert<Fruit>.ParseFromString(s); } }
希望对大家有帮助
相关阅读 更多 +