因项目需要花了点时间写了个枚举扩展类,详细内容如下:

 枚举声明:

public enum Status
{
[Description("邀请加入")]
邀请加入 = 1,
[Description("拒绝邀请")]
拒绝邀请 = 2,
[Description("申请加入")]
申请加入 = 3,
[Description("拒绝加入")]
拒绝加入 = 4,
[Description("同意加入")]
同意加入 = 5,
[Description("离开")]
离开 = 6
}

 

调用方法:

var temp = Convert.ToInt32(CmsRelationship.Status.同意加入); //普通使用
var temp1 = EnumExtension.GetValue<CmsRelationship.Status>("拒绝邀请"); //调用扩展类
var temp2 = EnumExtension.GetName<CmsRelationship.Status>(2);//调用扩展类

 

扩展类方法:


public static class EnumExtension
{

/// <summary>
/// 根据枚举类型的名称,获取枚举类型的数字值
/// </summary> 
public static int GetValue<T>(string inputString)
{
try
{
Type enumType = typeof(T);
var enumModel = Enum.Parse(enumType, inputString);
var result = Convert.ToInt32(enumModel);
return result;
}
catch
{
return 0;
}
}

 

/// <summary>
/// 根据枚举类型的数字值,获取枚举类型的名称
/// </summary> 
public static String GetName<T>(int input) 
{
try
{
var name = Enum.GetName(typeof(T), input);
return name;
}
catch
{
return "";
}
}

}

相关文章:

  • 2021-12-06
  • 2022-12-23
  • 2022-12-23
  • 2021-05-22
  • 2022-12-23
  • 2022-12-23
  • 2021-08-14
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2018-12-11
  • 2022-12-23
  • 2021-10-07
  • 2022-01-05
  • 2021-10-20
  • 2022-12-23
相关资源
相似解决方案