Program,Life,Society.....

导航

统计

公告

Enum操作技巧

示例:

VB:
Enum Colors
Red
Green
Blue
Yellow
End Enum

C#
enum class Colors{ Red, Green, Blue, Yellow};

1.得到枚举中常数值的数组。

VB:
For Each i In [Enum].GetValues(GetType(Coors))
Console.WriteLine(i)
Next i

C#:
for each(int i in Enum.GetValues(typeof(Colors)))
Console.WriteLine(i);

2. 检索指定枚举中常数名称的数组

VB:
For Each s In [Enum].GetNames(GetType(Colors))
Console.WriteLine(s)
Next s
C#
for each(string s in Enum.GetNames(typeof(Colors)))
Console.WriteLine(s);

3.转换Integer常数值和转换一个String常数名到Eumn实例

VB:
Public Function GetCoorsInstance(ByVal value As String) As Colors

Dim returnValue As Colors = CType([Enum].Parse(GetType(Colors), value), Colors)
Return returnValue

End Function

C#:
public Colors GetCoorsInstance(string value){

    Colors returnValue = (Colors)Enum.Parse(typeof(Colors), value);
    return returnValue;

}


写一个通用的。
VB:
Public Function GetEnumInstance(Of T)(ByVal enumType As T, ByVal value As String) As T

Dim returnValue As T = CType([Enum].Parse(GetType(T), value), T)

Return returnValue

End Function


C#
public T GetEnumInstance<T>(T enumType, string value){

    T returnValue = (T)Enum.Parse(typeof(T), value);
    return returnValue;

}

4.结合Attribute的到附加属性

Attribute类将预定义的系统信息或用户定义的自定义信息与目标元素相关联。创建一个继承自Attribute的类

VB:
Public Class StringValueAttribute

Inherits Attribute

Private _StringValue As String

Public Property StringValue() As String

Get

Return _StringValue

End Get

Protected Set(ByVal value As String)

_StringValue = value

End Set

End Property

Public Sub New(ByVal value As String)

Me.StringValue = value

End Sub

End Class

C#:
public
class StringValueAttribute : Attribute

{

    private string _StringValue;

    public string StringValue {

        get { return _StringValue; }

        protected set { _StringValue = value; }

    }

    public StringValueAttribute(string value)

    {

        this.StringValue = value;

    }

}

使用.NET3.5的新特性写一个针对Enum的扩展方法(添加System.Reflection引用),如果不是.NET3.5你可以写成一个函数,只不过调用的时候没有写成扩展来的方便

VB:
<System.Runtime.CompilerServices.Extension()> _

Module EnumHelper

<System.Runtime.CompilerServices.Extension()> _

Public Function GetStringValue(ByVal value As [Enum]) As String

' Get the type

Dim type As Type = value.[GetType]()

 

' Get fieldinfo for this type

Dim fieldInfo As FieldInfo = type.GetField(value.ToString())

 

' Get the stringvalue attributes

Dim attribs As StringValueAttribute() = TryCast(fieldInfo.GetCustomAttributes(GetType(StringValueAttribute), False), StringValueAttribute())

' Return the first if there was a match.

Return IIf(attribs.Length > 0, attribs(0).StringValue, Nothing)

End Function

End Module

C#:
[System.Runtime.CompilerServices.Extension()]

Class EnumHelper

{

    [System.Runtime.CompilerServices.Extension()]

    public string GetStringValue(Enum value)

    {

        // Get the type

        Type type = value.GetType();

 

        // Get fieldinfo for this type

        FieldInfo fieldInfo = type.GetField(value.ToString());

 

        // Get the stringvalue attributes

        StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(typeof(StringValueAttribute), false) as StringValueAttribute[];

        // Return the first if there was a match.

        return (attribs.Length > 0 ? attribs(0).StringValue : null);

    }

}

调用,定义你自己的Enum,并且在上面设置StringValueAttribute
定义一个U.S.P.S(美国邮政)的包裹Size
VB:
Public Enum Size

<StringValue("Package length plus girth must equal 84 inches or less")> _

Regular

<StringValue("Parcels that weigh less than 15 pounds but measure more than 84 inches but less than 108 inchess")> _

Large

<StringValue("Parcel Post packages that measure more than 108 inches but not more than 130 inches")> _

Oversize

End Enum

C#:
Public Enum Size
{

    [StringValue("Package length plus girth must equal 84 inches or less")]

    Regular,

    [StringValue("Parcels that weigh less than 15 pounds but measure more than 84 inches but less than 108 inchess")]

    Large,

    [StringValue("Parcel Post packages that measure more than 108 inches but not more than 130 inches")]

    Oversize

}

在程序中使用
VB:
Dim largeSize As Size = Size.Large
Dim value As String = largeSize.GetStringValue

C#:
Size largeSize = Size.Large;
string value = largeSize.GetStringValue

或者直接在Enum类型上使用
Size.Large.GetStringValue()

posted on 2008-09-12 17:00 rippleyong 阅读(...) 评论(...) 编辑 收藏