Obtains the description attribute of the enumerated variable value.
Source: Internet
Author: User
/// <Summary>
/// Obtain the description attribute of the enumerated variable value
/// </Summary>
/// <Param name = "OBJ"> enumeration variable </param>
/// <Param name = "Istop"> whether to change to the header description attribute of the returned class or enumeration type, instead of the current attribute or the description attribute of the enumerated variable value </param>
/// <Returns> If the description attribute is included, the value of the description attribute is returned. Otherwise, the name of the enumerated variable value is returned. </returns>
Public String getdescription (Object OBJ)
{
Bool Istop = false;
If (OBJ = NULL)
{
Return string. empty;
}
Try
{
Type _ enumtype = obj. GetType ();
Descriptionattribute DNA = NULL;
If (Istop)
{
DNA = (descriptionattribute) attribute. getcustomattribute (_ enumtype, typeof (descriptionattribute ));
}
Else
{
Fieldinfo Fi = _ enumtype. getfield (enum. getname (_ enumtype, OBJ ));
DNA = (descriptionattribute) attribute. getcustomattribute (
Fi, typeof (descriptionattribute ));
}
If (DNA! = NULL & string. isnullorempty (DNA. Description) = false)
Return DNA. description;
}
Catch
{
}
Return obj. tostring ();
}
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.