vue 得到枚举个数_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 sC#: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 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: _

Module EnumHelper

_

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(美国邮政)的包裹SizeVB:

Public

Enum Size

_

Regular

_

Large

_

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值