类似的东西可能工作:
private static IEnumerable<string> GetDescriptions(Type type)
{
var descs = new List<string>();
var names = Enum.GetNames(type);
foreach (var name in names)
{
var field = type.GetField(name);
var fds = field.GetCustomAttributes(typeof(DescriptionAttribute), true);
foreach (DescriptionAttribute fd in fds)
{
descs.Add(fd.Description);
}
}
return descs;
}
但是你可以查看一些逻辑存在:如是否确定要开始的名字?你将如何处理多个Description属性?如果他们中的一些人失踪了 - 你想要一个名字还是只是像上面那样跳过它?等
刚刚审查您的问题。对于VALUE你会有这样的事情:
private static IEnumerable<string> GetDescriptions(Enum value)
{
var descs = new List<string>();
var type = value.GetType();
var name = Enum.GetName(type, value);
var field = type.GetField(name);
var fds = field.GetCustomAttributes(typeof(DescriptionAttribute), true);
foreach (DescriptionAttribute fd in fds)
{
descs.Add(fd.Description);
}
return descs;
}
但它是不可能将两个说明单字段属性,所以我想它可能会返回字符串只。
嗨,那些似乎不是我的问题的答案。我需要列出所有基础枚举值。我可以得到一个单独的枚举的描述,或者如果我明确知道类型,而不是简单地传递Enum值。 – kodikas
啊,我明白你的意思了。你不能调用不仅仅是在其他答案中的代码,而是传入你的value.GetType()类型? –