首次提交:添加src文件夹代码
This commit is contained in:
228
Cowain.Bake.Common/Enums/EnumHelper.cs
Normal file
228
Cowain.Bake.Common/Enums/EnumHelper.cs
Normal file
@@ -0,0 +1,228 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.IO.Ports;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace Cowain.Bake.Common.Enums
|
||||
{
|
||||
public static class EnumHelper
|
||||
{
|
||||
public static string FetchDescription(this Enum value)
|
||||
{
|
||||
try
|
||||
{
|
||||
FieldInfo fi = value.GetType().GetField(value.ToString());
|
||||
if (null == fi)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
||||
if (attributes != null && attributes.Length > 0)
|
||||
return attributes[0].Description;
|
||||
else
|
||||
return value.ToString();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
public static string GetEnumDescription(this Enum @enum, int value)
|
||||
{
|
||||
Type enumType = @enum.GetType();
|
||||
if (Enum.IsDefined(enumType, value))
|
||||
{
|
||||
DescriptionAttribute[] descriptAttr = enumType.GetField(Enum.GetName(enumType, value)).GetDescriptAttr();
|
||||
return descriptAttr == null || descriptAttr.Length == 0 ? "" : descriptAttr[0].Description;
|
||||
}
|
||||
return "枚举异常";
|
||||
}
|
||||
|
||||
public static string GetDescription(this Enum enumName)
|
||||
{
|
||||
string str = string.Empty;
|
||||
DescriptionAttribute[] descriptAttr = enumName.GetType().GetField(enumName.ToString()).GetDescriptAttr();
|
||||
return descriptAttr == null || descriptAttr.Length == 0 ? enumName.ToString() : descriptAttr[0].Description;
|
||||
}
|
||||
public static ArrayList ToArrayList(this Enum en)
|
||||
{
|
||||
ArrayList arrayList = new ArrayList();
|
||||
foreach (Enum @enum in Enum.GetValues(en.GetType()))
|
||||
arrayList.Add(new KeyValuePair<Enum, string>(@enum, @enum.GetDescription()));
|
||||
return arrayList;
|
||||
}
|
||||
|
||||
public static DescriptionAttribute[] GetDescriptAttr(this FieldInfo fieldInfo)
|
||||
{
|
||||
if (fieldInfo != null)
|
||||
return (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
||||
return null;
|
||||
}
|
||||
public static dynamic Todynamic<T>(this Enum en)
|
||||
{
|
||||
Dictionary<int, string> valuePairs = new Dictionary<int, string>();
|
||||
foreach (Enum @enum in Enum.GetValues(en.GetType()))
|
||||
valuePairs.Add((int)Enum.Parse(typeof(T), @enum.ToString()), @enum.GetDescription());
|
||||
return valuePairs;
|
||||
}
|
||||
|
||||
public static dynamic ToStringKey<T>(this Enum en)
|
||||
{
|
||||
Dictionary<string, string> valuePairs = new Dictionary<string, string>();
|
||||
foreach (Enum @enum in Enum.GetValues(en.GetType()))
|
||||
valuePairs.Add(@enum.ToString(), @enum.GetDescription());
|
||||
return valuePairs;
|
||||
}
|
||||
public static dynamic GetListDesc(this Enum en)
|
||||
{
|
||||
List<string> lst = new List<string>();
|
||||
foreach (Enum @enum in Enum.GetValues(en.GetType()))
|
||||
lst.Add(@enum.GetDescription());
|
||||
return lst;
|
||||
}
|
||||
|
||||
public static dynamic ListKeyString<T>(this Enum en)
|
||||
{
|
||||
List<string> lst = new List<string>();
|
||||
foreach (Enum @enum in Enum.GetValues(en.GetType()))
|
||||
lst.Add(@enum.ToString());
|
||||
return lst;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <typeparam name="EnumType">The enum type.</typeparam>
|
||||
/// <param name="description">The description.</param>
|
||||
/// <returns>The enum value.</returns>
|
||||
public static EnumType GetValueByDescription<EnumType>(this string description)
|
||||
{
|
||||
var type = typeof(EnumType);
|
||||
foreach (var enumName in Enum.GetNames(type))
|
||||
{
|
||||
var enumValue = Enum.Parse(type, enumName);
|
||||
if (description == ((Enum)enumValue).GetDescription())
|
||||
return (EnumType)enumValue;
|
||||
}
|
||||
throw new ArgumentException("在指定的枚举类型值中没有此描述的值");
|
||||
}
|
||||
public static EnumType GetValueByKey<EnumType>(this string name)
|
||||
{
|
||||
var type = typeof(EnumType);
|
||||
foreach (var enumName in Enum.GetNames(type))
|
||||
{
|
||||
var enumValue = Enum.Parse(type, enumName);
|
||||
if (name == ((Enum)enumValue).ToString())
|
||||
return (EnumType)enumValue;
|
||||
}
|
||||
throw new ArgumentException("在指定的枚举类型值中没有此描述的值");
|
||||
}
|
||||
public static List<string> GetDescriptions<T>() where T : Enum
|
||||
{
|
||||
return Enum.GetValues(typeof(T))
|
||||
.Cast<T>()
|
||||
.Select(e => GetDescription(e))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public static List<EnumModel> GetEnumList<EnumType>()
|
||||
{
|
||||
var type = typeof(EnumType);
|
||||
List<EnumModel> enumModels = new List<EnumModel>();
|
||||
foreach (var enumName in Enum.GetNames(type))
|
||||
{
|
||||
EnumModel enumModel = new EnumModel();
|
||||
enumModel.EnumString= enumName;
|
||||
var enumValue = Enum.Parse(type, enumName);
|
||||
enumModel.EnumDesc= ((Enum)enumValue).GetDescription();
|
||||
enumModel.EnumIntValue = int.Parse(((Enum)enumValue).ToString("D"));
|
||||
enumModels.Add(enumModel);
|
||||
}
|
||||
return enumModels;
|
||||
}
|
||||
|
||||
public static Parity GetParity(string name)
|
||||
{
|
||||
Parity parity = Parity.None;
|
||||
var arr = System.Enum.GetValues(typeof(Parity));
|
||||
foreach (Parity item in arr)
|
||||
{
|
||||
if (item.ToString() == name)
|
||||
{
|
||||
parity = item;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return parity;
|
||||
}
|
||||
|
||||
public static StopBits GetStopBits(string name)
|
||||
{
|
||||
StopBits stopBits = StopBits.One;
|
||||
var arr = System.Enum.GetValues(typeof(StopBits));
|
||||
foreach (StopBits item in arr)
|
||||
{
|
||||
if (item.ToString() == name)
|
||||
{
|
||||
stopBits = item;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return stopBits;
|
||||
}
|
||||
|
||||
public static Handshake GetHandshake(string name)
|
||||
{
|
||||
Handshake handshake = Handshake.None;
|
||||
var arr = System.Enum.GetValues(typeof(Handshake));
|
||||
foreach (Handshake item in arr)
|
||||
{
|
||||
if (item.ToString() == name)
|
||||
{
|
||||
handshake = item;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return handshake;
|
||||
}
|
||||
|
||||
public static bool IsDefined<T>(int value) where T : struct, Enum
|
||||
{
|
||||
return Enum.IsDefined(typeof(T), value);
|
||||
}
|
||||
|
||||
//public static T GetNext<T>(this T value) where T : struct, Enum
|
||||
//{
|
||||
// var values = Enum.GetValues(typeof(T)).Cast<T>().ToArray();
|
||||
// int currentIndex = Array.IndexOf(values, value);
|
||||
// if (currentIndex < values.Length - 1)
|
||||
// {
|
||||
// return values[currentIndex + 1];
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// return values[0];
|
||||
// }
|
||||
//}
|
||||
|
||||
public static void GetEnum<T>(string a, ref T t)
|
||||
{
|
||||
foreach (T b in Enum.GetValues(typeof(T)))
|
||||
{
|
||||
if (GetDescription(b as Enum) == a)
|
||||
t = b;
|
||||
}
|
||||
}
|
||||
}
|
||||
public class EnumModel
|
||||
{
|
||||
public string EnumString { get; set; }
|
||||
public string EnumDesc { get; set; }
|
||||
public int EnumIntValue { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user