Ich versuche, die DisplayAttribute
Eigenschaften für eine enum
arbeiten, damit ich die verfügbaren Werte auflisten kann (um eine RESTful API verfügbar zu machen).Enum Reflection mit .NET Core
Ich habe eine Enumeration wie folgt:
/// <summary>
/// Available Proposal Types
/// </summary>
public enum ProposalTypes
{
Undefined = 0,
/// <summary>
/// Propose an administrative action.
/// </summary>
[Display(Name = "Administrative", Description = "Propose an administrative action.")]
Administrative,
/// <summary>
/// Propose some other action.
/// </summary>
[Display(Name = "Miscellaneous", Description = "Propose some other action.")]
Miscellaneous
}
ich dann einige Hilfsmethoden wie so gemacht:
/// <summary>
/// A generic extension method that aids in reflecting
/// and retrieving any attribute that is applied to an `Enum`.
/// </summary>
public static TAttribute GetAttribute<TAttribute>(this Enum enumValue) where TAttribute : Attribute
{
var type = enumValue.GetType();
var typeInfo = type.GetTypeInfo();
var attributes = typeInfo.GetCustomAttributes<TAttribute>();
var attribute = attributes.FirstOrDefault();
return attribute;
}
/// <summary>
/// Returns a list of possible values and their associated descriptions for a type of enumeration.
/// </summary>
/// <typeparam name="TEnum"></typeparam>
/// <returns></returns>
public static IDictionary<string, string> GetEnumPossibilities<TEnum>() where TEnum : struct
{
var type = typeof(TEnum);
var info = type.GetTypeInfo();
if (!info.IsEnum) throw new InvalidOperationException("Specified type is not an enumeration.");
var results = new Dictionary<string, string>();
foreach (var enumName in Enum.GetNames(type)
.Where(x => !x.Equals("Undefined", StringComparison.CurrentCultureIgnoreCase))
.OrderBy(x => x, StringComparer.CurrentCultureIgnoreCase))
{
var value = (Enum)Enum.Parse(type, enumName);
var displayAttribute = value.GetAttribute<DisplayAttribute>();
results[enumName] = $"{displayAttribute?.Name ?? enumName}: {displayAttribute?.Description ?? enumName}";
}
return results;
}
Die Verwendung hierfür wäre:
var types = Reflection.GetEnumPossibilities<ProposalTypes>();
Was scheint passiert sein, ist jedoch in der GetAttribute<TAttribute>
Methode, wenn ich versuche, das Attribut, das ich suche nach w i:
var attributes = typeInfo.GetCustomAttributes<TAttribute>();
... der resultierende Wert ist eine leere Enumeration und gibt somit einen Nullwert zurück. Von allem, was ich gelesen habe, sollte das gut funktionieren, und ich sollte die zugehörige DisplayAttribute
zurückbekommen ... aber ich bekomme einen Nullwert zurück.
Was mache ich falsch?
Aus Neugier, was machst du mit diesem Wörterbuch (Rückgabewert von 'GetEnumPossibilities') am Ende? Wenn du das irgendwo als JSON zurückbringst, durchläufst du unnötig viel Ärger. JSON.NET wird Ihre Attribute korrekt serialisieren. –
@HristoYankov Ich verwende dies, um einen Endpunkt zu erstellen, der die möglichen Typen definiert, um ein API-Consumer-Frontend zu informieren. –