[英]How to pass the x in typeof(x) as a method parameter when x is not a variable but an enum type
I have the following enum 我有以下枚举
public enum StatusEnum
{
Open= 1,
SemiOpen = 2,
Closed= 3
}
I pass it in my ASP.NET 5 view to my custom HTML Helper 我将其在ASP.NET 5视图中传递给自定义HTML帮助器
@Html.EnumDropDownListFor(m => m.SwitchStatus, typeof (StatusEnum), "- Please select Item -")
which is a method that uses Generic enum as parameter 这是使用通用枚举作为参数的方法
public static IHtmlContent EnumDropDownListFor<TModel, TResult, TEnum>(
this IHtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TResult>> expression,
TEnum enumValue,
string optionLabel)
{
// bunch of logic omitted as not relevant to error
//calling another method passing the TEnum
return null;
}
which works. 哪个有效。
However I need to pass the enum type between a number of methods as I need to treat it as an TEnum
and not a Type which typeof
will pass. 但是,我需要在多种方法之间传递枚举类型,因为我需要将其视为
TEnum
而不是要传递typeof
的Type。
From the above I add a where clause to tell the method the TEnum is an enum. 从上面我添加了一个where子句来告诉方法TEnum是一个枚举。
public static IHtmlContent EnumDropDownListFor<TModel, TResult, TEnum>(
this IHtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TResult>> expression,
TEnum enumValue,
string optionLabel) where TEnum : struct, IConvertible, IFormattable
{
// bunch of logic omitted as not relevant to error
//calling another method passing the TEnum
return null;
}
However then I get a red line in my view under 但是然后我在下面的视图中看到一条红线
@Html.EnumDropDownListFor
in Visual Studio 在Visual Studio中
showing the error message 显示错误信息
Error CS0453 The type 'Type' must be a non-nullable value type in order to use it as parameter 'TEnum' in the generic type or method EnumDropDownListFor
错误CS0453类型“类型”必须是不可为空的值类型,以便在通用类型或方法EnumDropDownListFor中将其用作参数“ TEnum”
Basically as I understand it I need to pass it a TEnum
and not a Type
基本上,据我了解,我需要将其传递给
TEnum
而不是Type
However if I remove the typeof
from my line 但是如果我从行中删除
typeof
@Html.EnumDropDownListFor(m => m.SwitchStatus, StatusEnum, "- Please select Item -")
I understandably get 我可以理解
StatusEnum is a type which is not valid in the given context
StatusEnum是在给定上下文中无效的类型
What I basically want to do is do is call typeof
inside the method like this 我基本上想做的是在像这样的方法内部调用
typeof
public static IHtmlContent EnumDropDownListFor<TModel, TResult, TEnum>(
this IHtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TResult>> expression,
TEnum enumValue,
string optionLabel) where TEnum : struct, IConvertible, IFormattable
{
Type enumValue2 = typeof(StatusEnum);
// bunch of logic omitted as not relevant to error
//calling another method passing the TEnum
return null;
}
and for that I need the MVC View to look something like 为此,我需要MVC视图看起来像
@Html.EnumDropDownListFor(m => m.SwitchStatus, Before_typeof(StatusEnum), "- Please select Item -")
so I am allowed to pass it and do the typeof
in the method. 所以我可以通过它并在方法中执行
typeof
。
Is this possible? 这可能吗?
Take note: I can get it working with the non generic version 注意:我可以将其与非通用版本一起使用
ie 即
public static IHtmlContent EnumDropDownListFor<TModel, TResult>(
this IHtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TResult>> expression,
Type enumValue,
string optionLabel)
{
//logic
return null;
}
which works perfectly 完美地工作
I just wanted to know if the generic version is possible somehow 我只是想知道通用版本是否可能
When you pass in typeof (StatusEnum)
as second parameter, the TEnum
becomes a Type
, which isn't what you want. 当您将
typeof (StatusEnum)
作为第二个参数传递时, TEnum
变为Type
,这不是您想要的。 It seems to me you want to put in the value of an enum in there, since the field is named enumValue
. 在我看来,您想在其中放入枚举的值 ,因为该字段名为
enumValue
。 So you should pass in StatusEnum.Okay
, or something like that, not a type. 因此,您应该传入
StatusEnum.Okay
或类似的内容,而不是类型。
You already have the type from the type parameter, so if it is to do about the type, you don't need to pass in that parameter at all. 您已经具有type参数中的类型,因此,如果要处理该类型,则根本不需要传入该参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.