首页  ·  知识 ·  编程语言
随心所欲操作Enum枚举类型
zhaowt001  博客园http://www.cnblogs.com/zhaowt001/  .NET  编辑:德仔   图片来源:网络
"font-size: 9pt; color: blue; font-family: 新宋体">简单的写了一个操作E

简单的写了一个操作Enum枚举类型的Helper,实现如下功能:
1、由Int值可以得到Enum对象值
2、由String值可以得到Enum对象值
3、由Hex值可以得到Enum对象值
4、Hex、ASCII值<-得到->Enum对象值


至于其应用,首先是方便写程序的时候对其进行操作;其次是用在自定义协议上,简化协议字符,但是写程序时却可以清晰的知道每个ASCII代表的Enum是什么意思;可以用在UDP传输协议上,压缩传输字符串。

using System;
 
 namespace HxH.Collections
 {
     public enum ExampleNormalEnum
     {
         Online    = 1,            //    1    in DB
         Offline    = 2,            //    2    in DB
         Hide    = 3,            //    3    in DB
         Busy    = 4,            //    4    in DB
         Away    = 5,            //    5    in DB
         Other    = 6,            //    6    in DB
     }
 
     public enum ExampleHexEnum
     {
         Login                = 0x22,        //登陆服务器    ASCII = "
         LogOff                = 0x23,        //退出服务器    ASCII = #
         Online                = 0x24,        //在线            ASCII = $
         Offline                = 0x25,        //下线            ASCII = %
         Away                = 0x26,        //离开            ASCII = &
         Busy                = 0x27,        //            ASCII = '
         Hide                = 0x28,        //隐身            ASCII = (
     }
 
    ///
    /// EnumHelper 的摘要说明。
    ///
    public class EnumHelper
    {
        public EnumHelper()
        {
        }
 
        ///
        /// 从Enum中任意取一个Int值,将其转化成枚举类型值
        ///
        ///
        ///
        ///
        /// ExampleNormalEnum status = (ExampleNormalEnum)EnumHelper.IntValueToEnum( typeof( ExampleNormalEnum ),1); 得到值为 ExampleNormalEnum.Online
         public static object IntValueToEnum( System.Type protocolType,int enumIntValue)
         {
             object myObject = Enum.Parse( protocolType,Enum.GetName( protocolType, enumIntValue ));
           return myObject;
       }
 
       ///
       /// 从Enum中任意取一个String值,将其转化成枚举类型值
       ///
       ///
       ///
       ///
        /// ExampleNormalEnum status = (ExampleNormalEnum)EnumHelper.StringValueToEnum( typeof( ExampleNormalEnum ),"Offline");得到值为 ExampleNormalEnum.Offline
         public static object StringValueToEnum( System.Type protocolType,string enumStringValue)
      {
          object myObject = Enum.Parse( protocolType,enumStringValue,true);
          return myObject;
      }
 
      ///
      /// 得到一个Enum中的所有Int值
      ///
      ///
      ///
      public static int[] GetEnumIntValues( System.Type protocolType )
      {
          int[] myIntArray = new int[ Enum.GetValues( protocolType ).Length ];
          Array.Copy( Enum.GetValues( protocolType ),myIntArray,Enum.GetValues( protocolType ).Length );
            return myIntArray;
        }
 
        ///
        /// 静态方法,根据枚举类型返回ASCII的字符串值
        ///
        /// 枚举类型
        /// 枚举值
        /// ASCII字符串值
        /// EnumHelper.EnumValueToASCIIString( typeof( ExampleHexEnum ),ExampleHexEnum.Hide );得到的值为"("
       public static string EnumValueToASCIIString( System.Type protocolType ,object objectValue)
         {
             return HexStringToASCIIString( EnumValueToHexString( protocolType,objectValue ) );
        }
4
        ///
        /// 输入16进制的字符串,返回翻译成ASCII的字符串
        ///
        ///
        ///
        /// EnumHelper.HexStringToASCIIString( "2A" ); 得到值为"*",注意去掉16进制前置标志符号"0x"
      public static string HexStringToASCIIString(string hexString)
      {
            int myInt16 = int.Parse( hexString,System.Globalization.NumberStyles.AllowHexSpecifier);
            char myChar = (char)myInt16;
            return myChar.ToString();
        }
 
        ///
    /// 静态方法,根据枚举类型返回16进制的字符串值
       ///
       ///
       ///
       ///
        /// EnumHelper.EnumValueToHexString(typeof( ExampleHexEnum ),ExampleHexEnum.Hide);得到值"00000028"
        public static string EnumValueToHexString( System.Type protocolType,object objectValue)
        {
            return Enum.Format( protocolType,
                Enum.Parse( protocolType,
                Enum.GetName( protocolType,objectValue ) ),"X" );
        }
 
 
        ///
        /// 将ASCII字符串转换成 Enum 的值
        ///
        ///
        ///
        ///
        /// EnumHelper.ASCIIStringToEnumValue( typeof( ExampleHexEnum ),"(") 得到值 "ExampleHexEnum.Hide"
        public static object ASCIIStringToEnumValue( System.Type protocolType,string asciiString)
        {
            return HexStringToEnumValue( protocolType, ASCIIStringToHexString( asciiString ));
        }
 
        ///
        /// 输入ASCII的字符串,翻译成16进制的字符串
        ///
        ///
        ///
        /// EnumHelper.ASCIIStringToHexString( "(" ); 得到值"28"
        public static string ASCIIStringToHexString(string normalString)
        {
            System.Text.Encoding enc = System.Text.Encoding.GetEncoding("ASCII");
 
            for( int i=0;i
            {
                byte[] bs = enc.GetBytes( normalString[i].ToString() );
                for( int j=0;j
                {
                    return bs[j].ToString("X2");
                }
            }
 
            return "FF";
        }
       
        ///
        /// 将16进制转换为 Enum 的值
        ///
        ///
        ///
        ///
        /// EnumHelper.HexStringToEnumValue( typeof( ExampleHexEnum ),"28");得到值 "ExampleHexEnum.Hide"
       public static object HexStringToEnumValue( System.Type protocolType,string hexString )
        {
            object myObject = Enum.Parse( protocolType,
                Enum.GetName( protocolType ,
                Int16.Parse( hexString ,System.Globalization.NumberStyles.AllowHexSpecifier) ) );
 
            return myObject;
        }
    }
}
本文作者:zhaowt001 来源:博客园http://www.cnblogs.com/zhaowt001/
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读