2021-07-23 09:42:22 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
2022-10-28 22:45:39 +08:00
|
|
|
|
namespace AIStudio.Wpf.DiagramDesigner
|
2021-07-23 09:42:22 +08:00
|
|
|
|
{
|
|
|
|
|
|
public static class EnumExtension
|
|
|
|
|
|
{
|
2022-12-04 23:07:20 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Converts to enum.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="str">The string.</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static T ToEnum<T>(this string str)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (T)Enum.Parse(typeof(T), str);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-23 09:42:22 +08:00
|
|
|
|
public static string GetDescription(this Enum value)
|
|
|
|
|
|
{
|
|
|
|
|
|
FieldInfo field = value.GetType().GetField(value.ToString());
|
|
|
|
|
|
DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
|
|
|
|
|
|
|
|
|
|
|
|
return attribute == null ? value.ToString() : attribute.Description;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|