69 lines
1.8 KiB
C#
69 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DHSoftware
|
|
{
|
|
public static class EnumHelper
|
|
{
|
|
public enum SelectPicType
|
|
{
|
|
[Description("训练图片")]
|
|
train =0,
|
|
[Description("测试图片")]
|
|
test
|
|
|
|
}
|
|
public enum NetModel
|
|
{
|
|
[Description("目标检测")]
|
|
training = 0,
|
|
[Description("语义分割")]
|
|
train_seg,
|
|
[Description("模型导出")]
|
|
emport,
|
|
[Description("推理预测")]
|
|
infernce
|
|
|
|
}
|
|
public static string GetEnumDescription(this Enum enumObj)
|
|
{
|
|
Type t = enumObj.GetType();
|
|
FieldInfo f = t.GetField(enumObj.ToString());
|
|
if (f == null)
|
|
{
|
|
return enumObj.ToString();
|
|
}
|
|
DescriptionAttribute attr = f.GetCustomAttribute<DescriptionAttribute>();
|
|
if (attr != null)
|
|
{
|
|
return attr.Description;
|
|
}
|
|
else
|
|
{
|
|
return enumObj.ToString();
|
|
}
|
|
}
|
|
|
|
// 根据描述获取枚举值(泛型方法)
|
|
public static T GetEnumFromDescription<T>(string description) where T : Enum
|
|
{
|
|
Type enumType = typeof(T);
|
|
foreach (T value in Enum.GetValues(enumType))
|
|
{
|
|
string desc = GetEnumDescription(value);
|
|
if (desc == description)
|
|
{
|
|
return value;
|
|
}
|
|
}
|
|
throw new ArgumentException($"在枚举 {enumType.Name} 中未找到描述为 '{description}' 的值");
|
|
}
|
|
|
|
}
|
|
}
|