miepzerino
2025-03-29 91e32d7af1911b98858fc85808ae38a89f05ff06
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using Assets.Scripts.Enums;
using System;
using System.Reflection;
 
namespace Assets.Scripts.Helpers
{
    internal static class EnumHelpers
    {
 
        /// <summary>
        /// Will get the string value for a given enums value, this will
        /// only work if you assign the StringValue attribute to
        /// the items in your enum.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetStringValue(this Enum value)
        {
            // Get the type
            Type type = value.GetType();
 
            // Get fieldinfo for this type
            FieldInfo fieldInfo = type.GetField(value.ToString());
 
            // Get the stringvalue attributes
            StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
                typeof(StringValueAttribute), false) as StringValueAttribute[];
 
            // Return the first if there was a match.
            return attribs.Length > 0 ? attribs[0].StringValue : null;
        }
    }
}