指定Enum默认值

写一个EnumDefaultValue的范例:

 

    [AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
    public class EnumDefaultValueAttribute : Attribute
    {

    }

 

class Program

    {

       static voidMain(string[] args)

       {

           //指定默认枚举

           int defalutValue;

           bool isDefulat=TestEnum.GetDefaultEnum(typeof(EnumA), out defalutValue);

           if (isDefulat)

           {

               Console.Write(defalutValue);

           }

           else

           {

               //默认枚举为最大值或最小值

               int min=TestEnum.GetMin(typeof(EnumA));

               int max=TestEnum.GetMax(typeof(EnumA));

               Console.Write(min);

               Console.Write(max);

           }

           Console.Read();

       }

    }

   

    public enum EnumA

    {

       AAA = 1,

[EnumDefaultValue]

       BBB = 2

    }

 

    public class TestEnum

    {

       public static bool GetDefaultEnum(TypeenumType, out intdefaultValue)

       {

           if (!enumType.IsEnum)

           {

               throw new ArgumentException("enumType无效");

           }

 

           FieldInfo[] fields = enumType.GetFields();

           foreach (FieldInfofield in fields)

           {

               if (field.FieldType.IsEnum)

               {

                   object[] itemAttributes =field.GetCustomAttributes(typeof(EnumDefaultValueAttribute), true);

                   if (itemAttributes != null&& itemAttributes.Length == 1)

                   {

                       EnumDefaultValueAttribute itemAttribute =itemAttributes[0] as EnumDefaultValueAttribute;

                       if (itemAttribute != null)

                       {

                           object enumItemObj =enumType.InvokeMember(field.Name, BindingFlags.GetField,null, null, null);

                           defaultValue = (int)enumItemObj;

                           return true;

                       }

                   }

               }

           }

 

           defaultValue = int.MinValue;

           return false;

       }

       public static int GetMin(TypeenumType)

       {

           if (!enumType.IsEnum)

           {

               throw new ArgumentException("enumType无效");

           }

 

           int min = int.MaxValue;

           Array a = Enum.GetValues(enumType);

           foreach (varitem in a)

           {

               int itemValue=(int)item;

               if (itemValue < min)

               {

                   min = itemValue;

               }

           }

           return min;

       }

       public static int GetMax(TypeenumType)

       {

           if (!enumType.IsEnum)

           {

               throw new ArgumentException("enumType无效");

           }

 

           int max = int.MinValue;

           Array a = Enum.GetValues(enumType);

           foreach (varitem in a)

           {

               int itemValue = (int)item;

               if (itemValue > max)

               {

                   max = itemValue;

               }

           }

           return max;

       }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值