In order just to skip pesky Event0Instance.
prefix can you try using static
construction which works roughly similar to with .. end with
:
using static MyNamespace.Event0Instance;
...
public static Dictionary<Event0Instance, string> Event0Descriptions =
new Dictionary<Event0Instance, string>()
{
{ClaimSent, "Подана претензия" },
{RequestSent, "Подан запрос"},
{ClaimResponseAqcuired, "Получен ответ на претензию"},
{RequestResponseAqcuired, "Получен ответ на запрос"},
{RequestRefusalAcquired, "Получен отказ на запрос"}
};
However, I suggest using attributes and build dictionary with a help of Linq and Reflection:
// Here we attribute each option with the desired description
public enum Event0Instance
{
//TODO: do not hardcode the descriptions ("Подана претензия")
// but put resource name here
[Description("Подана претензия")]
ClaimSent,
[Description("Подан запрос")]
RequestSent,
[Description("Получен ответ на претензию")]
ClaimResponseAqcuired,
[Description("Получен ответ на запрос")]
RequestResponseAqcuired,
[Description("Получен отказ на запрос")]
RequestRefusalAcquired,
}
Here is the routine to inspect the enum given and returns {option, description}
dictionary:
using System.Linq;
using System.Reflection;
...
// This routine inspects the enum given and return dictionary: {option, description}
public static Dictionary<T, string> EnumDescriptions<T>() where T : Enum {
return Enum
.GetNames(typeof(T))
.Select(name => new {
name,
value = Enum.Parse(typeof(T), name),
member = typeof(T).GetMember(name)[0]
})
.ToDictionary(item => (T) (item.value),
item => item.member
.GetCustomAttributes<DescriptionAttribute>(true)
.FirstOrDefault()?.Description ?? item.name);
}
and then you can build the dictionaries in one line of code:
// I doubt if you want public (!) Dictionary exposed to everyone;
// I guess readonly interface will be enough
public static readonly IReadOnlyDictionary<Event0Instance, string> Event0Descriptions =
EnumDescriptions<Event0Instance>();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…