无法将枚举扩展方法与泛型一起使用

Unable to use Enum extension method with generic

我知道泛型是在编译时完成的,但我对泛型的工作方式感到困惑(虽然我知道泛型..)。

我创建了以下扩展方法:

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 使用它,returns 一个字符串(html 文本),就像这个一样。请注意我的 class 可以访问我的 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,但如何做到这一点并使其保持通用?

提前致谢!

UPDATE:感谢第一个答案,我能够通过将 TModel 限制为 struct, IConvertible

来编译

您的方法需要一个约束。做,

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");
}

我希望原因很明显。

因为你的扩展方法是为Twhere T : struct, IConvertible定义的。

但是 CheckBoxesForEnumModel 中的 TModel 不符合这些通用类型约束。

您应该更改签名

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

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

或更严格。

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

但是在您的 HtmlHelper 扩展方法中,您的 TModel 没有相同的约束,因此编译器无法根据 struct 和 [=14= 类型关联您的枚举扩展方法] 你的 TModel 只是一种类型。

将相同的约束添加到您的 HtmlHelper 方法就可以解决问题:

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