Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.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# 自定义属性-获取";不是有效的命名属性参数";对于枚举_C#_Asp.net Mvc_Attributes_Custom Attributes - Fatal编程技术网

C# 自定义属性-获取";不是有效的命名属性参数";对于枚举

C# 自定义属性-获取";不是有效的命名属性参数";对于枚举,c#,asp.net-mvc,attributes,custom-attributes,C#,Asp.net Mvc,Attributes,Custom Attributes,当我尝试使用MultiselectComperer值时,为什么会收到此消息: [Display(ResourceType = typeof(OrdersManagementStrings), Name = "PrintSettings")] [FilterAttribute(IsMultiselect = true, MultiselectComperer=FilterAttribute.eMultiselectComperer.Or)] public ePrintSettings PrintS

当我尝试使用
MultiselectComperer
值时,为什么会收到此消息:

[Display(ResourceType = typeof(OrdersManagementStrings), Name = "PrintSettings")]
[FilterAttribute(IsMultiselect = true, MultiselectComperer=FilterAttribute.eMultiselectComperer.Or)]
public ePrintSettings PrintSettings { get; set; }
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
public sealed class FilterAttribute : Attribute
{
    public enum eMultiselectComperer
    {
        Unspecified = 0, 
        Or,
        And
    }

    public bool IsMultiselect { get; set; }

    public eMultiselectComperer MultiselectComperer { get; set; }
}
这是自定义属性的代码。。。所有的电动车都是公共的。。但我得到了这个信息:

“MultiselectComperer”不是有效的命名属性参数,因为 它不是有效的属性参数 键入


问题是,
MultiselectComperer
属性可为空。编译器正在抱怨,因为很遗憾,您无法生成可为null类型的常量。如果您使其不可为null,那么您的类将正常工作

如果需要向
eMultiselectComperer
表示第三个值,则可以创建第三个枚举值作为该枚举的默认值,如下所示:

[Display(ResourceType = typeof(OrdersManagementStrings), Name = "PrintSettings")]
[FilterAttribute(IsMultiselect = true, MultiselectComperer=FilterAttribute.eMultiselectComperer.Or)]
public ePrintSettings PrintSettings { get; set; }
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
public sealed class FilterAttribute : Attribute
{
    public enum eMultiselectComperer
    {
        Unspecified = 0, 
        Or,
        And
    }

    public bool IsMultiselect { get; set; }

    public eMultiselectComperer MultiselectComperer { get; set; }
}

这样,如果用户在声明属性时没有为
MultiselectComperer
属性指定值,它将默认为
Unspecified
(或您喜欢的任何名称)。

糟糕的标题,顺便说一句。自定义属性呢?这没有帮助。。正如你所说,我已经删除了nullable,我仍然收到同样的信息。@vertalus你确定吗?您可能需要进行清理和重建。我已经在LINQPad的一个示例程序中尝试了这段代码,它编译得很好。非常wierd。。我已重建,但仍有此错误。。我稍后会尝试清理+重建..谢谢,这对我稍微不同的情况有所帮助。我试图使用DateTime,虽然它不可为null,但也不是静态的。我切换到字符串,并在构造函数中解析为日期。