2017-02-20 30 views
2

我试图让DisplayAttribute属性的enum工作,所以我可以列出可用值(暴露给一个RESTful API)。枚举的反思与.NET核心

我有一个枚举如下:

/// <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 
} 

我再发像一些辅助方法这样:

/// <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; 
    } 

它的用法是:

var types = Reflection.GetEnumPossibilities<ProposalTypes>(); 

似乎什么但是,在GetAttribute<TAttribute>方法中,当我尝试获取我正在查找的属性时ith:

var attributes = typeInfo.GetCustomAttributes<TAttribute>(); 

...结果值是一个空的枚举,因此返回一个空值。从我读过的所有内容中,应该可以正常工作,并且我应该找回相关联的DisplayAttribute ...但我得到一个空值。

我在做什么错?

+0

出于好奇,你在做什么这个词典(GetEnumPossibilities'的返回值)在最后?如果你以JSON的形式返回这个地方,你会不必要地遇到很多麻烦。 JSON.NET将正确序列化你的属性。 –

+0

@HristoYankov我正在使用它来创建一个端点,它定义了可能的类型以通知API消费者前端。 –

回答

4

问题是您正在寻找ProposalTypes类型的属性,而不是类型的值。 See this Question了解有关获取枚举值的属性的信息。

更确切地说,在GetAttribute中,您需要获得代表您的具体价值的成员并致电GetCustomAttributes。你的方法将看起来像这样:

public static TAttribute GetAttribute<TAttribute>(this Enum enumValue) where TAttribute : Attribute 
{ 
    var type = enumValue.GetType(); 
    var typeInfo = type.GetTypeInfo(); 
    var memberInfo = typeInfo.GetMember(enumValue.ToString()); 
    var attributes = memberInfo[0].GetCustomAttributes<TAttribute>(); 
    var attribute = attributes.FirstOrDefault(); 
    return attribute; 
} 
+1

嗯。我想我正在查看一个枚举值作为它自己的类型,这不是C#所看到的。感谢您的帮助,这非常令人沮丧。 –

+0

是的,我可以看到你如何从这种假设开始有麻烦。我的调试提示是,如果你看看GetType返回的是什么,那么你很可能已经指出了正确的方向,认识到这些值本身不是类型。我不得不去查看如何从属性值中获取属性。 ;-) – Chris