[英]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中使用它,它返回一个像这样的字符串(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");
}
我知道我可以获得所有Enum值并使用TModel迭代它们,如下所示:
TModel[] allEnumValues = (TModel[])Enum.GetValues(typeof(TModel));
但如果我知道TModel是一个枚举(它是一个MyEnumType),为什么我不能用它来访问枚举扩展方法,如:
allValues[0].GetDescription<>(); //ERROR. this does not compile
我想这是因为不知怎的,我必须将它转换为像MyEnumType这样的特定类型,但是如何这样做并保持通用?
提前致谢!
更新 :感谢第一个答案,我能够通过将TModel
限制为struct, IConvertible
进行编译
因为你的扩展方法是为T定义的, where 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
或更严格的。
您的方法需要约束。 做,
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");
}
我希望原因很明显。
您基于T
为您的枚举创建了一个扩展方法,其中T
是struct
它实现了IConvertible
。
但在你的HtmlHelper扩展方法您的TModel不具有相同的限制,所以没有办法是编译器可以基于一个类型,是你的枚举扩展方法关联struct
和IConvertible
你的TModel这仅仅是一个类型。
在HtmlHelper方法中添加相同的约束就可以了:
public static IHtmlString CheckBoxesForEnumModel<TModel>(this HtmlHelper<TModel> htmlHelper) where TModel : struct, IConvertible
{
string description = htmlHelper.ViewData.Model.GetDescription();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.