//内部类
public static class EnumHelper
{
public static string GetDescription(Enum value)
{
if (value == null)
{
throw new ArgumentException("value");
}
string description = value.ToString();
var fieldInfo = value.GetType().GetField(description);
var attributes =
(EnumDescriptionAttribute[]) fieldInfo.GetCustomAttributes(typeof (EnumDescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
{
description = attributes[0].Description;
}
return description;
}
}

// 枚举定义
// 调用方法
String strKufen = EnuHelper.GetDescription(MessageInfo.Send); 
public enum MessageInfo
{
[EnumDescription("Send")]
Send,
[EnumDescription("Received")]
Received
} [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public sealed class EnumDescriptionAttribute : Attribute
{
private string description;
public string Description { get { return description; } } public EnumDescriptionAttribute(string description)
: base()
{
this.description = description;
}
}

c# 枚举返回字符串操作的更多相关文章

  1. 转-C#让枚举返回字符串

    下面的手段是使用给枚举项打标签的方式,来返回字符串 下面分别定义一个属性类,和一个枚举帮助类 [AttributeUsage(AttributeTargets.Field,AllowMultiple  ...

  2. C# 让枚举返回字符串

    下面的手段是使用给枚举项打标签的方式,来返回字符串 分别定义一个属性类,一个枚举帮助类 /// <summary> /// 自定义属性 /// </summary> [Attr ...

  3. C# 格式化字符串,日期,字符串操作汇总

    时间格式化 有时候我们要对时间进行转换,达到不同的显示效果 默认格式为:2005-6-6 14:33:34 如果要换成成200506,06-2005,2005-6-6或更多的该怎么办呢 我们要用到:D ...

  4. Python之字符串操作

    一.字符串特点 内容不可修改 password=' #内容不可修改 二.字符串常用方法 1..strip()方法 去字符串两边的空格和换行符 print(password.strip()) #去掉字符 ...

  5. C# 字符串操作详解

    MSDN关于String的所有Method 1.字符串转字符数组 (1).ToCharArray()方法,源码如下: 调用代码: var str = "Hello World"; ...

  6. C# 字符串操作基本过程(Equals、Compare、EndsWith等处理方法)

    本文只介绍了比较方法,但是EndsWith,IndexOf等方法均采用相同的过程,先设置CultureInfo(一般情况下调用当前线程的CultureInfo,该语言文化可以通过控制面板设置),然后调 ...

  7. 如何使用T-SQL备份还原数据库及c#如何调用执行? C#中索引器的作用和实现。 jquery控制元素的隐藏和显示的几种方法。 localStorage、sessionStorage用法总结 在AspNetCore中扩展Log系列 - 介绍开源类库的使用(一) span<T>之高性能字符串操作实测

    如何使用T-SQL备份还原数据库及c#如何调用执行? 准备材料:Microsoft SQL Server一部.需要还原的bak文件一只 一.备份 数据库备份语句:user master backup ...

  8. Python3基础(2)模块、数据类型及运算、进制、列表、元组、字符串操作、字典

    ---------------个人学习笔记--------------- ----------------本文作者吴疆-------------- ------点击此处链接至博客园原文------ 1 ...

  9. python学习笔记(字符串操作、字典操作、三级菜单实例)

    字符串操作 name = "alex" print(name.capitalize()) #首字母大写 name = "my name is alex" pri ...

随机推荐

  1. centos7下使用wget命令安装mysql

    1.首先安装wget命令: yum -y install  wget 2.下载mysql wget http://repo.mysql.com/mysql-community-release-el7- ...

  2. NOI模拟题1 Problem A: sub

    题面 Sample Input 5 7 2 -1 -3 1 1 1 2 1 3 3 4 3 5 2 1 3 0 2 1 2 1 2 1 1 -3 2 Sample Output 2 4 5 2 HIN ...

  3. eclipse中修改JDK版本

    eclipse中,一个java项目依赖的JDK,需要进行绑定,但绑定的地方会有多个,类似层级结构. 1. eclipse的window -> preferences -> java com ...

  4. django admin页面样式丢失问题

    wamp 配置django admin页面样式丢失问题 第一种方法:在apache配置文件httpd.conf中加入如下代码:Alias /static "E:\Python27\Lib\s ...

  5. 更换 mac时 开发证书

    更换 mac时 开发证书,先导出, 后导入,如果出现错误,把projects自动签名改为手动

  6. ylbtech-czgfh(规范化)-数据库设计

    ylbtech-DatabaseDesgin:ylbtech-czgfh(规范化)-数据库设计 DatabaseName:czgfh(财政规范化) Model:账户模块.系统时间设计模块.上报自评和审 ...

  7. 用CHttpFile实现简单的GET/POST数据【转】

    一.GET 数据,下载网页,文件等,用于可下载的文件,不能用于服务端运行的程序,比如.aspx文件等,否则会返回500错误. CString strSentence, strWriteName=&qu ...

  8. apache的配置优化

    [APACHE的工作方式] prefork模式(默认)这个多路处理模块(MPM)实现了一个非线程型的.预派生的web服务器,它的工作方式类似于Apache 1.3.它适合于没有线程安全库,需要避免线程 ...

  9. jquery ajax传参数问题

    var fd = new FormData();//实例化表单,提交数据使用fd.append('imgUrl',imgUrl);//将files追加进去fd.append('typeId',type ...

  10. same-tree——比较两个二叉树是否相同

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...