Get Enum String Value - CSharp System

CSharp examples for System:Enum

Description

Get Enum String Value

Demo Code


using System.Reflection;
using System;/*  ww  w.j a v a2 s .c o  m*/

public class Main{
        public static string GetStringValue(this Enum value)
        {
            Type type = value.GetType();

            FieldInfo fieldInfo = type.GetRuntimeField(value.ToString());

            StringValueAttribute[] attribs =
                fieldInfo.GetCustomAttributes(typeof(StringValueAttribute), false) as StringValueAttribute[];

            //return the first
            return attribs.Length > 0 ? attribs[0].Value : null;
        }
}

Related Tutorials