コード例 #1
0
        public static EnumMetadataAttribute GetMetadata(object stringEnum, Type type)
        {
            if (type.GetCustomAttribute(typeof(StringEnumAttribute)) == null)
            {
                return(null);
            }
            int                   index    = Array.IndexOf(Enum.GetValues(type), stringEnum);
            var                   fields   = type.GetFields().Where(x => x.IsStatic).ToArray();
            FieldInfo             field    = fields[index];
            EnumMetadataAttribute metadata = (EnumMetadataAttribute)field.GetCustomAttribute(typeof(EnumMetadataAttribute));

            return(metadata);
        }
コード例 #2
0
        public static EnumMetadataAttribute GetMetadata <T>(T stringEnum)
            where T : Enum
        {
            Type type = typeof(T);

            if (type.GetCustomAttribute(typeof(StringEnumAttribute)) == null)
            {
                return(null);
            }
            FieldInfo             field    = type.GetFields().Where(x => x.IsStatic).ToArray()[Array.IndexOf(Enum.GetValues(type), stringEnum)];
            EnumMetadataAttribute metadata = (EnumMetadataAttribute)field.GetCustomAttribute(typeof(EnumMetadataAttribute));

            return(metadata);
        }
コード例 #3
0
        public static object GetEnumValueFromString(string str, Type type)
        {
            foreach (FieldInfo fieldInfo in type.GetFields())
            {
                if (!fieldInfo.IsStatic)
                {
                    continue;
                }
                EnumMetadataAttribute metadata = fieldInfo.GetCustomAttribute(typeof(EnumMetadataAttribute)) as EnumMetadataAttribute;
                if (metadata == null)
                {
                    continue;
                }
                if (metadata.Identifier == str)
                {
                    return(fieldInfo.GetValue(null));
                }
            }

            return(null);
        }
コード例 #4
0
        public static T?GetValueFromString <T>(string str)
            where T : struct, Enum
        {
            foreach (FieldInfo fieldInfo in typeof(T).GetFields())
            {
                if (!fieldInfo.IsStatic)
                {
                    continue;
                }
                EnumMetadataAttribute metadata = fieldInfo.GetCustomAttribute(typeof(EnumMetadataAttribute)) as EnumMetadataAttribute;
                if (metadata == null)
                {
                    continue;
                }
                if (metadata.Identifier == str)
                {
                    return((T?)fieldInfo.GetValue(null));
                }
            }

            return(null);
        }