C#中可以对枚举类型用Description特性描述. 如果需要对Description信息获取,那么可以定义一个扩展方法来实现.代码如下: public static class EnumExtensions { public static string GetDescription(this object value) { if (value==null) return string.Empty; Type type = value.GetType(); var fieldInfo = ty
关键代码: using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Reflection; namespace CSharpUtilHelpV2 { /// <summary> /// 基于.NET 2.0的枚举工具类 /// </summary> public static class EnumToolV2
/// <summary> /// 获取枚举Description的Name /// </summary> /// <param name="enumName">枚举</param> /// <returns></returns> public static string GetDescription(Enum enumName) { var name = enumName.ToString(); var type
//把一个对象转换成功键值对字典格式 var obj = new { CustomerId = customerId }; var dic = obj.ToDictionray(); public static class ObjectExtension { public static Dictionary<string, object> ToDictionary(this object obj) { if (obj == null) r
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Prog
Enum使用 获取枚举属性 注意:扩展方法必须定义为静态类,静态方法中. public enum EnumPatientSource { [Description("住院")] INHOSPITAL = -1, [Description("门诊")] OUTPATIENT = 0, } public static class EnumHelper { public static string ToDescription(this Enum val) { var ty
一.定义一个类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Reflection; namespace XXX.XXX.Utils { /// <summary> /// 备注特性 /// </summary> public class DescAttribute : A
代码: public class EnumberHelper { public static List<EnumberEntity> EnumToList<T>() { List<EnumberEntity> list = new List<EnumberEntity>(); foreach (var e in Enum.GetValues(typeof(T))) { EnumberEntity m = new EnumberEntity(); object