Пример #1
0
        /// <summary>
        /// Prepares the parsing for a specific enum by returning a pre-computed dictionary with all allowed mapping name =&gt; enum.
        /// </summary>
        /// <typeparam name="T">
        /// Type of the enum to compute the dictionary
        /// </typeparam>
        /// <returns>
        /// A pre-computed dictionary with all allowed mapping name =&gt; enum
        /// </returns>
        protected static StringEnumMap PrepareParsing <T>() where T : CompositeEnum
        {
            var map = new StringEnumMap();

            var type = typeof(T);

            while (type != typeof(CompositeEnum))
            {
                foreach (var field in type.GetFields())
                {
                    if (typeof(CompositeEnum).GetTypeInfo().IsAssignableFrom(field.FieldType.GetTypeInfo()) && field.IsStatic)
                    {
                        var fieldValue = (CompositeEnum)field.GetValue(null);
                        if (fieldValue.Values.Count == 1)
                        {
                            var key = fieldValue.Values.FirstOrDefault();
                            if (key != null && key.Key != null && !map.ContainsKey(key.Key))
                            {
                                map.Add(key.Key, fieldValue);
                            }
                        }
                    }
                }

                type = type.GetTypeInfo().BaseType;
            }

            return(map);
        }