惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Archives - TechRepublic
Security Archives - TechRepublic
罗磊的独立博客
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
Apple Machine Learning Research
Apple Machine Learning Research
The Register - Security
The Register - Security
J
Java Code Geeks
V2EX - 技术
V2EX - 技术
Vercel News
Vercel News
N
News and Events Feed by Topic
腾讯CDC
P
Proofpoint News Feed
N
News | PayPal Newsroom
www.infosecurity-magazine.com
www.infosecurity-magazine.com
爱范儿
爱范儿
O
OpenAI News
酷 壳 – CoolShell
酷 壳 – CoolShell
月光博客
月光博客
Martin Fowler
Martin Fowler
Engineering at Meta
Engineering at Meta
D
Docker
Y
Y Combinator Blog
博客园 - 聂微东
G
Google Developers Blog
S
Security @ Cisco Blogs
Simon Willison's Weblog
Simon Willison's Weblog
S
Schneier on Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
SegmentFault 最新的问题
云风的 BLOG
云风的 BLOG
阮一峰的网络日志
阮一峰的网络日志
C
CXSECURITY Database RSS Feed - CXSecurity.com
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
CERT Recently Published Vulnerability Notes
I
Intezer
G
GRAHAM CLULEY
有赞技术团队
有赞技术团队
Attack and Defense Labs
Attack and Defense Labs
V
Visual Studio Blog
博客园 - Franky
博客园 - 三生石上(FineUI控件)
W
WeLiveSecurity
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Hugging Face - Blog
Hugging Face - Blog
Scott Helme
Scott Helme
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
L
LINUX DO - 最新话题
C
Cybersecurity and Infrastructure Security Agency CISA

博客园 - vuejs3

More on SQL Server Service Broker [转]Android试验:如果View的ID相同会出现什么效果? 父子关系排序,无确定根节点 Silverlight 操作技巧 [文摘]怎么使用Sticky Footer代码(让页脚紧贴页面底部的方法) Sharepoint development toolbox 通过对象模型上传List Template - vuejs3 - 博客园 在文档库中隐藏多文件上传/Disable the Upload Multiple Document option in Document Library - vuejs3 [转]SharePoint Document Library and List – File Upload ASP.NET 2.0 TreeView控件在IE7中断开的连接线 Team Foundation Server讲义 通过CertEnroll在CA上(1创建证书请求2得到证书3安装证书) 如何在WSS中利用KeywordQuery创建搜索查询 SharePoint Web Service的身份验证 SharePoint开发中对ListViewWebPart的几个操作 【转】免费SharePoint资源 LINQ概述-通用和便利的信息查询方式 NetShopForge网上商店程序(VB)源码—讨论-发布 Microsoft AJAX添加自定义智能感知(intellisense)
Enum操作技巧
vuejs3 · 2008-09-12 · via 博客园 - vuejs3

示例:

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