62,046
社区成员
发帖
与我相关
我的任务
分享
public static object EnumParse(Type type, object value, object defaultValue)
{
object obj = null;
try
{
obj = Enum.Parse(type, value.ToString());
}
catch
{
return defaultValue;
}
if (obj.ToString() == "0" || ConvertToInt(obj.ToString()) != 0)
return defaultValue;
else
return obj;
}
public static int ConvertToInt(string s)
{
try
{
return Convert.ToInt16(s);
}
catch
{
return 0;
}
}
Color color = Enum.IsDefined(typeof(Color), str) ? (Color)Enum.Parse(typeof(Color), str) : Color.red;
public static T StringToEnum<T>(string str)
{
return (T)Enum.Parse(typeof(T), str, false);
}
public static class aaa<T>
{
public static T GetEnumByValue<T>(this string source)
{
if (typeof(T).BaseType == typeof(Enum))
{
foreach (T value in Enum.GetValues(typeof(T)))
{
if (source == value.ToString())
{
return value;
}
}
}
else
{
throw new ArgumentException("T必须为枚举类型");
}
return default(T);
}
public enum Colors
{
black,
red,
blue
}
Colors obj = "blue".GetEnumByValue<Colors>();
------------------------
}