. NET enumeration (using notes)

Source: Internet
Author: User

To get a comment class:

<summary>

Notes Properties
</summary>
public class Remarkattribute:attribute
{
private string M_remark;
Public Remarkattribute (string remark)
{
This.m_remark = remark;
}
<summary>
Note
</summary>
public string Remark
{
get {return m_remark;}
set {M_remark = value;}
}
<summary>
Gets the comment information for the enumeration
</summary>
<param name= "Val" > Enumeration values </param>
<returns></returns>
public static string Getenumremark (Enum val)
{
Try
{
Type type = val. GetType ();
FieldInfo fd = type. GetField (Val. ToString ());
if (FD = = null)
return string. Empty;
object[] Attrs = FD. GetCustomAttributes (typeof (Remarkattribute), false);
String name = String. Empty;
foreach (Remarkattribute attr in Attrs)
{
Name = attr. Remark;
}
return name;
}
catch (Exception ex)
{
Log4NetHelper.Log.Error ("Getenumremark Erro", ex);
return null;
}
}
}
<summary>
Enumerating extension Classes
</summary>
public static Class Enumextension
{
<summary>
Gets the comment information for the enumeration
</summary>
<param name= "em" ></param>
<returns></returns>
public static string Getremark (this Enum em)
{
Try
{
Type type = em. GetType ();
FieldInfo fd = type. GetField (em. ToString ());
if (FD = = null)
return string. Empty;
object[] Attrs = FD. GetCustomAttributes (typeof (Remarkattribute), false);
String name = String. Empty;
foreach (Remarkattribute attr in Attrs)
{
Name = attr. Remark;
}
return name;
}
catch (Exception ex)
{
Log4NetHelper.Log.Error ("Getremark Error", ex);
return null;
}

}

Enum class:

public enum Codeemun
{
<summary>
Success
</summary>
[Remark ("Success")]
SUCCESS = 1,
<summary>
Failed
</summary>
[Remark ("failure")]
FAILED = 0,
<summary>
XML Format error
</summary>
[Remark ("XML Format error")]
Xmlformaterror = 2,
<summary>
Signature Error
</summary>
[Remark ("signature error")]
Singerror = 3,
<summary>
References to objects that do not exist (for example, an order number with a wrong jest)
</summary>
[Remark ("reference to non-existent object (e.g. wrong jest order number)]
Orderiderror = 101,
<summary>
Business status is not correct
</summary>
[Remark ("Business status is incorrect")]
Statuserror = 102,
<summary>
Business cannot execute due to business constraints
</summary>
[Remark ("Business cannot be performed due to business constraints")]
Yewulimit = 103,
<summary>
Real-name authentication failed
</summary>
[Remark ("real name Authentication failed")]
Identification = 104
}

Call Method:

Codeemun Enumitem = (Codeemun) enum.parse (typeof (Investmentcommon.codeemun), notifyInfo.Code.ToString ());
String codemessage = Remarkattribute.getenumremark (Enumitem);

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

. NET enumeration (using notes)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.