Get Enum Name Value - CSharp System

CSharp examples for System:Enum

Description

Get Enum Name Value

Demo Code


using System.Xml.Serialization;
using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.IO;/*from w w w  . j  a  va2  s .c o  m*/
using System.Collections.Generic;
using System;

public class Main{
        public static String GetNameValue(Enum value)
        {
            var attribute = (XmlEnumAttribute)Attribute.GetCustomAttribute(value.GetType().GetField(value.ToString()), typeof(XmlEnumAttribute));
            String result;

            if (attribute != null)
            {
                result = attribute.Name;
            }
            else
            {
                result = value.ToString();
            }

            return result;
        }
}

Related Tutorials