1. Defining enum types
public enum Commstatus
{
<summary>
UDP type
</summary>
[Description ("UDP type")] UDP = 0,
<summary>
UDP type
</summary>
[Description ("UDP broadcast type")]
Udpbroadcast = 1,
<summary>
TCP Service-side type
</summary>
[Description ("TCP Service-side type")] TCPServer = 2,
<summary>
TCP Client Type
</summary>
[Description ("TCP client Type")] TcpClient = 3
}
2. Defining custom Attribute Types
public class Descriptionattribute:attribute
{
Public DescriptionAttribute (String desc)
{
Description = desc;
}
public string Description {get; set;}
}
3. Get custom comment content for enum properties by reflection
public class Enumdescription
{
public static string Getenumdescription<tenum> (object value)
{
var enumtype = typeof (Tenum);
if (!enumtype.isenum)
{
throw new ArgumentException ($ "Enumitem req{value. ToString ()}uires a Enum ");
}
var name = Enum.getname (Enumtype, Convert.ToInt32 (value));
if (name = = NULL)
Return "";
var objs = Enumtype.getfield (name). GetCustomAttributes (typeof (DescriptionAttribute), false);
var attr = objs[0] as DescriptionAttribute;
if (OBJS. Length = = 0)
{
return string. Empty;
}
Else
{
return attr! = null? attr. Description:string. Empty;
}
}
}
4, you need to use the enumeration of the custom comment place directly call the method getenumdescription to the enumeration parameters can be
Annotation properties for custom enumeration types are used for UI display