csharp util csharp
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp util csharp相关的知识,希望对你有一定的参考价值。
public static T StringToEnum<T>(this string name)
{
return (T)Enum.Parse(typeof(T), name);
}
public static string ToDescriptionString(this Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute),
false);
if (attributes != null &&
attributes.Length > 0)
return attributes[0].Description;
else
return value.ToString();
}
public static int ToInt(this Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute),
false);
if (attributes != null &&
attributes.Length > 0)
return Convert.ToInt32(attributes[0].Description);
else
return Convert.ToInt32(value.ToString());
}
以上是关于csharp util csharp的主要内容,如果未能解决你的问题,请参考以下文章