无法使用泛型枚举扩展方法

4

我知道泛型是在编译时完成的,但是我对泛型的工作方式感到困惑(我曾经认为我了解泛型..)。

我创建了以下扩展方法:

public static class EnumExt
{
    /// <summary>
    /// Gets the description, if any, or the name of the enum as a string in a enum type
    /// </summary>
    public static string GetDescription<T>(this T enumType) where T : struct, IConvertible
    {
        FieldInfo fieldInfo = enumType.GetType().GetField(enumType.ToString());
        DescriptionAttribute[] descriptionAttributes = (DescriptionAttribute[])
            fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
        if (descriptionAttributes.Length > 0)
        {
            return descriptionAttributes[0].Description;
        }
        return enumType.ToString();
    }
}

我举一个例子,以下是枚举类内容:

namespace MyProject.Model
{
    [Flags]
    public enum MyEnumType
    {
        [Description("None")]
        None = 0,
        [Description("Show Products (default)")]
        Products  = 1,
        [Description("Show Tariffs")]
        Tariffs = 2
    }
}

现在我想在MVC中使用HttpHelper来返回一个字符串(html文本),如下所示。请注意,我的类可以访问我的EnumExt方法。

    public static IHtmlString CheckBoxesForEnumModel<TModel>(this HtmlHelper<TModel> htmlHelper)
    {
        if (!typeof(TModel).IsEnum)
        {
            throw new ArgumentException("this helper can only be used with enums");
        }
        TModel[] allEnumValues = (TModel[])Enum.GetValues(typeof(TModel));
        foreach (TModel item in allEnumValues)
        {
            var descErr = item.GetDescription(); //does not compile, but I know it's a MyEnumType.Tariffs..
            var descOk = MyEnumType.Tariffs.GetDescription(); //this line works
            //descOk = "Show Tariffs"
        }

        return new HtmlString("ideally this is some html checkboxes with each enum description");
    }

我知道可以使用TModel获取所有枚举值并遍历它们,方法如下:

TModel[] allEnumValues = (TModel[])Enum.GetValues(typeof(TModel));

但是,如果我知道 TModel 是一个枚举类型(它是 MyEnumType),为什么我不能使用它来访问这个枚举类型的扩展方法呢?

allValues[0].GetDescription<>(); //ERROR. this does not compile

我想这是因为我必须将它转换为特定类型(如MyEnumType),但如何保持泛型呢?
提前致谢!
更新:感谢第一次回答,我能够通过限制TModel为struct、IConvertible来编译。

1
似乎 TModel 也应该是 struct, IConvertble - tukaef
是否可能由于将TModel限制为struct, IConvertible并且无法使之成为枚举,因此我不能再使用GetDescription枚举扩展方法,因为它不再是“真正的”枚举? 我可以编译但item.GetDescription()没有返回“Show Tariffs”,而是“Tariffs”。 - diegosasw
3个回答

3

因为您的扩展方法是针对T where T : struct, IConvertible进行定义的。

CheckBoxesForEnumModel中的TModel没有符合这些泛型类型约束。

您应该更改签名,从

public static IHtmlString CheckBoxesForEnumModel<TModel>(this HtmlHelper<TModel> htmlHelper)

to

public static IHtmlString CheckBoxesForEnumModel<TModel>(this HtmlHelper<TModel> htmlHelper)
    where TModel : struct, IConvertible

更为严格。

我回复了nawfal并提供了适用于你的解决方案的答案: 谢谢。通过您的更改,它可以编译,但是扩展方法中的T似乎具有descriptionAttributes == 0,因此它不能按预期工作,因此它返回字符串“Tariffs”,而不是根据其描述属性返回“Show Tariffs”。我仍然缺少一些东西 :( - diegosasw
噢..我运行的版本没有描述。谢谢并抱歉。就像你说的那样,一切都好。 - diegosasw

2

你的方法需要一个约束条件。请执行以下操作:

public static IHtmlString CheckBoxesForEnumModel<TModel>(this HtmlHelper<TModel> htmlHelper) where TModel : struct, IConvertible
{
   if (!typeof(TModel).IsEnum)
   {
      throw new ArgumentException("this helper can only be used with enums");
   }

   //Here some code to get all the values and the names for this Enum        
   //But HOW?? 

   return new HtmlString("ideally this is some html checkboxes with each enum description");
}

我希望原因是显而易见的。

1

您为枚举创建了一个基于T的扩展方法,其中Tstruct并实现了IConvertible

但在您的HtmlHelper扩展方法中,您的TModel没有相同的约束条件,因此编译器无法将基于structIConvertible类型的枚举扩展方法与仅为类型的TModel相关联。

将相同的约束条件添加到HtmlHelper方法中即可解决问题:

public static IHtmlString CheckBoxesForEnumModel<TModel>(this HtmlHelper<TModel> htmlHelper) where TModel : struct, IConvertible
{
    string description = htmlHelper.ViewData.Model.GetDescription();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接