枚举类型和int以及string的相互转换
2010-03-04 13:31 Virus-BeautyCode 阅读(2385) 评论(0) 编辑 收藏 举报
假如我们现在有一个枚举类型
枚举
/// <summary>
/// 用户类型
/// </summary>
public enum UserTypeEnum
{
/// <summary>
/// 匿名用户
/// </summary>
Anonymous = 1,
/// <summary>
/// 学生
/// </summary>
Student,
/// <summary>
/// 教师
/// </summary>
Teacher,
/// <summary>
///系统平台
/// </summary>
SystemPlatform
}
/// 用户类型
/// </summary>
public enum UserTypeEnum
{
/// <summary>
/// 匿名用户
/// </summary>
Anonymous = 1,
/// <summary>
/// 学生
/// </summary>
Student,
/// <summary>
/// 教师
/// </summary>
Teacher,
/// <summary>
///系统平台
/// </summary>
SystemPlatform
}
类型转换工具类
代码
/// <summary>
/// 用户类型转换,枚举类型和int、string类型的转换
/// </summary>
public class UserTypeEnumUtility
{
public static UserTypeEnum String2UserType(string text,bool ignoreCase)
{
return (UserTypeEnum)Enum.Parse(typeof(UserTypeEnum), text,ignoreCase );
}
public static int UserType2Int(UserTypeEnum userType)
{
return (int)userType ;
}
public static UserTypeEnum Int2UserType(int n)
{
if (Enum.IsDefined(typeof(UserTypeEnum), n))
return (UserTypeEnum)n;
else
throw new Exception(string.Format("{0} is not definded.", n));
}
public static string UserType2String(UserTypeEnum userType)
{
return userType.ToString();
}
}
/// 用户类型转换,枚举类型和int、string类型的转换
/// </summary>
public class UserTypeEnumUtility
{
public static UserTypeEnum String2UserType(string text,bool ignoreCase)
{
return (UserTypeEnum)Enum.Parse(typeof(UserTypeEnum), text,ignoreCase );
}
public static int UserType2Int(UserTypeEnum userType)
{
return (int)userType ;
}
public static UserTypeEnum Int2UserType(int n)
{
if (Enum.IsDefined(typeof(UserTypeEnum), n))
return (UserTypeEnum)n;
else
throw new Exception(string.Format("{0} is not definded.", n));
}
public static string UserType2String(UserTypeEnum userType)
{
return userType.ToString();
}
}