private static KeyValuePair <T, string> CreateKeyValuePair <T>(FieldInfo field) where T : struct { T value = (T)field.GetValue(null); ApiValueAttribute nameAttribute = (ApiValueAttribute)field.GetCustomAttribute(typeof(ApiValueAttribute)); string name = nameAttribute?.Value ?? value.ToString().ToUpperInvariant(); return(new KeyValuePair <T, string>(value, name)); }
static FlagsEnumMap() { if (!typeof(T).GetTypeInfo().IsDefined(typeof(FlagsAttribute))) { throw new InvalidOperationException($"{typeof(T).Name} is not marked with the {typeof(FlagsAttribute).Name} attribute. Use the {nameof(EnumMap<T>)} class or the {nameof(EnumMap.ToApiValue)} method instead."); } s_intValueToString = ApiValueAttribute.GetApiValueNamesIn <T>().ToDictionary(vn => (int)(object)vn.Key, vn => vn.Value); }