简单的写了一个操作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/