现在的位置: 首页 > 综合 > 正文

c# enum用法 枚举用法 Asp.net ENUM用法

2012年03月30日 ⁄ 综合 ⁄ 共 2535字 ⁄ 字号 评论关闭

原文:c# enum用法 枚举用法 Asp.net ENUM用法

enum 关键字用于声明枚举,即一种由一组称为枚举数列表的命名常数组成的独特类型。每种枚举类型都有基础类型,该类型可以是除char 以外的任何整型。即:

(byte, sbyte, short, ushort, int, uint, long和ulong)

Emumeration 这个概念早在C时代就有了,不过以前没怎么用过。

基本表达, 改变默认值和默认类型

Enumeration的默认值是从0开始的int,如下: 
enum Direction
{
   UP,
   RIGHT,
DOWN,
   LEFT,
};
此时UP=0, RIGHT=1...依此类推

改变默认值:
enum Direction
{
   UP=1,
   RIGHT=2,
DOWN=3,
   LEFT=4,
};

改变类型(只能改变成:byte, sbyte, short, ushort, int, uint, long, ulong):
enum Direction : long
{
    UP = 1111111111,
    DOWN = 1111111112,
    LEFT = 1111111113,
    RIGHT = 1111111114
};

访问Enumeration变量的值

赋值前先cast(强制类型转换):
long direct = (long)Direction.UP;

Enumeration变量的文字描述

如果想要Enumeration返回一点有意义的string,从而用户能知道分别代表什么, 则按如下定义:
using System.ComponentModel; // 先添加该引用
enum Direction
{
    [Description("this means facing to UP (Negtive Y)")]
    UP = 1,
    [Description("this means facing to RIGHT (Positive X)")]
    RIGHT = 2,
    [Description("this means facing to DOWN (Positive Y)")]
    DOWN = 3,
    [Description("this means facing to LEFT (Negtive X)")]
    LEFT = 4
};

使用如下方法来获得文字描述:
using System.Reflection;
using System.ComponentModel;
public static String GetEnumDesc(Direction e)
{
FieldInfo EnumInfo = e.GetType().GetField(e.ToString());
DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[]) EnumInfo.
        GetCustomAttributes (typeof(DescriptionAttribute), false);
if (EnumAttributes.Length > 0)
    {
return EnumAttributes[0].Description;
    }
return e.ToString();
}

或者可以自己定义Discription Attributes
enum Direction
{
    [EnumDescription("Rover is facing to UP (Negtive Y)")]
    UP = 1,
    [EnumDescription("Rover is facing to DOWN (Positive Y)")]
    DOWN = 2,
    [EnumDescription("Rover is facing to RIGHT (Positive X)")]
    RIGHT = 3,
    [EnumDescription("Rover is facing to LEFT (Negtive X)")]
    LEFT = 4
};

AttributeUsage(AttributeTargets.Field)]

public class EnumDescriptionAttribute : Attribute
{
private string _text = "";
public string Text
    {
get { return this._text; }
    }
public EnumDescriptionAttribute(string text)
    {
        _text = text;
    }

}

虽然现在,.net 中关于 enum 的 ToString 操作会输出这个 enum 的名字,不过,有很多情况下,我们希望使用不同的语言来显示它,或者,我们想要显示的字符串中有非法字符(比如空格,对于变量来讲,是非法字符,而对于显 示来说,就是一个普通的要求了),而这个属性,帮助我们显示 enum 所定义的字符串,不过,很可惜的,虽然任何的 enum 都是从 System.Enum 派生的,不过,我们没有办法改写它的 ToString 函数,所以,需要一个 Helper 类来帮助它的输出:

public class EnumStringHelper
{
public static string ToString(object o)
    {
Type t = o.GetType();
string s = o.ToString();
EnumDescriptionAttribute[] os = (EnumDescriptionAttribute[])t.GetField(s).GetCustomAttributes(typeof(EnumDescriptionAttribute), false);
if (os != null && os.Length == 1)
        {
return os[0].Text;
        }
return s;
    }
}

使用上,类似这样:
static void Main(string[] args)
{
    Direction myDirection = Direction.DOWN;
string s = EnumStringHelper.ToString(myDirection);
    Console.WriteLine("{0}", s);
    Console.ReadKey();
}

抱歉!评论已关闭.