Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当x不是变量而是枚举类型时,如何在typeof(x)中将x作为方法参数传递_C#_Generics_Enums - Fatal编程技术网

C# 当x不是变量而是枚举类型时,如何在typeof(x)中将x作为方法参数传递

C# 当x不是变量而是枚举类型时,如何在typeof(x)中将x作为方法参数传递,c#,generics,enums,C#,Generics,Enums,我有下面的枚举 public enum StatusEnum { Open= 1, SemiOpen = 2, Closed= 3 } 我在ASP.NET 5视图中将其传递给我的自定义HTML助手 @Html.EnumDropDownListFor(m => m.SwitchStatus, typeof (StatusEnum), "- Please select Item -") 这是一个使用泛型枚举作为参数的方法 public static IHtmlC

我有下面的枚举

public enum StatusEnum
{
    Open= 1,
    SemiOpen = 2,
    Closed= 3
}
我在ASP.NET 5视图中将其传递给我的自定义HTML助手

@Html.EnumDropDownListFor(m => m.SwitchStatus, typeof (StatusEnum), "- Please select Item -")
这是一个使用泛型枚举作为参数的方法

  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;
    }
然而,我得到了一条红线,在我的看法下

@Html.EnumDropDownListFor 
在VisualStudio中

显示错误消息

错误CS0453类型“type”必须是不可为null的值类型,才能将其用作泛型类型或方法EnumDropDownListFor中的参数“TEnum”

基本上按照我的理解,我需要传递一个
TEnum
而不是
类型

但是,如果我从行中删除
typeof

 @Html.EnumDropDownListFor(m => m.SwitchStatus, StatusEnum, "- Please select Item -")
我可以理解

StatusEnum是在给定上下文中无效的类型

我主要想做的是在方法内部调用
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;
    }
因此,我可以通过它并在方法中执行
typeof

这可能吗


注意:我可以让它与非通用版本一起工作

i、 e

公共静态IHtmlContent EnumDropDownList for(
这个IHtmlHelper htmlHelper,
表情表情,
类型枚举值,
字符串选项标签)
{
//逻辑
返回null;
}
这很好用


我只是想知道,当您传入
typeof(StatusEnum)
作为第二个参数时,
TEnum
会变成
类型,这不是您想要的。在我看来,您似乎想在其中输入枚举的值,因为该字段名为
enumValue
。所以您应该传入
StatusEnum.ok
,或者类似的内容,而不是类型


您已经从type参数中获得了类型,因此如果要处理该类型,您根本不需要传入该参数。

可能的重复我已经更新了我的帖子,并提供了一个传递“type”工作的示例。我只是想知道是否有可能使用TEnum泛型而不是非泛型方法是的。将TEnum与Where子句编号结合使用。这都在我的原始问题中,但是
Where
的目的是什么?
TEnum
必须是
struct
等。?当您传入
类型时,请参见此线程。
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;
    }
 @Html.EnumDropDownListFor(m => m.SwitchStatus, Before_typeof(StatusEnum), "- Please select Item -")
    public static IHtmlContent EnumDropDownListFor<TModel, TResult>(
         this IHtmlHelper<TModel> htmlHelper,
         Expression<Func<TModel, TResult>> expression,
         Type enumValue,
         string optionLabel)
    {
        //logic
        return null;

    }