C# Enum Name String Description之间的相互转换
最近工作中经常用到Enum中Value、String、Description之间的相互转换,特此总结一下。
1.首先定义Enum对象
public enum Weekday
{
[Description("星期一")]
Monday=,
[Description("星期二")]
Tuesday=,
[Description("星期三")]
Wednesday=,
[Description("星期四")]
Thursday=,
[Description("星期五")]
Friday=,
[Description("星期六")]
Saturday=,
[Description("星期日")]
Sunday=
}
2.根据string类型的值,获取相应的对象,例如:“Sunday”获取Weekday.Sunday.
public static Weekday GetEnumFromString(string str)
{
Weekday week;
bool b = Enum.TryParse(str, true, out week);
if (b)
return week;
else
throw new Exception("没有找到对应的Enum");
}
3.根据Value类型的值,获取对应的description。
public static String GetEnumDescription(Weekday week)
{ FieldInfo field = week.GetType().GetField(week.ToString());
object[] obj = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
DescriptionAttribute da;
if (obj != null && obj.Length > )
{
da = (System.ComponentModel.DescriptionAttribute)obj[];
return da.Description;
}
else
throw new Exception("没有找到对应的Description");
}
4.根据Description的值,获取对应的Value。
public static Weekday GetEnumFromDescription(string description)
{
foreach(Weekday week in Enum.GetValues(typeof(Weekday)))
{
if (description.Equals(GetEnumDescription(week))==true)
return week;
}
throw new Exception("没有找到对应的Enum");
}
C# Enum Name String Description之间的相互转换的更多相关文章
- 包装类、基本数据类型及String类之间的相互转换
包装类:8种基本数据类型对应一个类,此类即为包装类 一.基本数据类型 包装类 及String之间的转换 1.基本数据类型转化为包装类:调用包装类的构造器 int i=10; Inte ...
- c++中char*\wchar_t*\string\wstring之间的相互转换
string U2A(const wstring& str)//Unicode字符转Ascii字符 { string strDes; if ( str.empty() ) goto __end ...
- C#中List〈string〉和string[]数组之间的相互转换
1,从System.String[]转到List<System.String> System.String[] str={"str","string" ...
- java int和String类型之间的相互转换
String --> int 第一种方法:int i = Integer.parseInt(s); 第二种方法:int i = Integer.valueOf(s).intValue(); 两种 ...
- 关于InputStream 和String对象之间的相互转换
代码如下: package com.xin.stream; import java.io.BufferedReader; import java.io.ByteArrayInputStream; im ...
- inputStream、File、Byte、String等等之间的相互转换
一:inputStream转换 1.inputStream转为byte //方法一 org.apache.commons.io.IOUtils包下的实现(建议) IOUtils.toByteArray ...
- data和string类型之间的相互转换
package main; import java.text.SimpleDateFormat;import java.util.Date; import freemarker.core.ParseE ...
- List<string>和string[]数组之间的相互转换,需要的朋友可以参考下
1,从System.String[]转到List<System.String> System.String[] str={"str","string" ...
- 基本数据类型、包装类、String类型之间的相互转换
@Testpublic void test2(){//基本数据类型.包装类-->到String类型的转换,调用String类型的静态方法valueOf()即可int i1 = 12;String ...
随机推荐
- ABP理论学习之仓储
返回总目录 本篇目录 IRepository接口 查询 插入 更新 删除 其他 关于异步方法 仓储实现 管理数据库连接 仓储的生命周期 仓储最佳实践 Martin Fowler对仓储的定义 位于领域层 ...
- 剑指Offer面试题:33.二叉树的深度
一.题目一:二叉树的深度 1.1 题目说明 题目一:输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.例如下图中的二叉树的 ...
- SQL Server 深入解析索引存储(下)
标签:SQL SERVER/MSSQL SERVER/数据库/DBA/索引体系结构/非聚集索引 概述 非聚集索引与聚集索引具有相同的 B 树结构,它们之间的显著差别在于以下两点: 基础表的数据行不按非 ...
- Azure PowerShell (9) 使用PowerShell导出订阅下所有的Azure VM的Public IP和Private IP
<Windows Azure Platform 系列文章目录> 笔者在之前的工作中,有客户提出想一次性查看Azure订阅下的所有Azure VM的Public IP和Private IP. ...
- zookeeper分布式锁原理
一.分布式锁介绍分布式锁主要用于在分布式环境中保护跨进程.跨主机.跨网络的共享资源实现互斥访问,以达到保证数据的一致性. 二.架构介绍在介绍使用Zookeeper实现分布式锁之前,首先看当前的系统架构 ...
- 不知道张(zhāng)雱(pāng)是谁?你out了!
张(zhāng)雱(pāng)是谁?也许你已经听说过了,也许你还没听说过呢,不过你一定听说过老刘——刘强东,没错,这二人是有关系的,什么关系,京东是老刘的,而张雱呢?张雱是京东旗下52家关联公司法人代 ...
- CATransition转场动画
背景: 最近在温习动画,分享个简单系统的转场动画 viewcontroller *VC=[self.storyboard instantiateViewControllerWithIdentifier ...
- Angularjs在控制器(controller.js)的js代码中使用过滤器($filter)格式化日期/时间实例
Angularjs内置的过滤器(filter)为我们的数据信息格式化提供了比较强大的功能,比如:格式化时间,日期.格式化数字精度.语言本地化.格式化货币等等.但这些过滤器一般都是在VIEW中使用的,比 ...
- LINQ系列:Linq to Object排序操作符
LINQ排序操作符包括:OrderBy.OrderByDescending.ThenBy.ThenByDescending及Reverse. 1. OrderBy 1>. 原型定义 public ...
- 配置 L3 agent - 每天5分钟玩转 OpenStack(99)
上一节我们介绍了路由服务(Routing)的基本功能,今天教大家如何配置. Neutron 的路由服务是由 l3 agent 提供的. 除此之外,l3 agent 通过 iptables 提供 fir ...