简体   繁体   English

使用枚举作为属性参数

[英]Using an Enum as an Attribute Argument

Here is the code I would like to use: 这是我要使用的代码:

public enum Days { Sat = 1, Sun, Mon, Tue, Wed, Thu, Fri };

[EnumHelper(typeof(Days))]
public Days DayOfWeek { get; set; }

EnumHelper looks like: EnumHelper看起来像:

[AttributeUsage(AttributeTargets.Property,AllowMultiple=true)]
public class EnumHelper : Attribute
{
    public Type MyEnum { get; set; }
    public EnumHelper(Type enum)
    {
        MyEnum = enum;
    }
}

The error I get on EnumHelper(Days) is that "Enum Name not valid at this point". 我在EnumHelper(Days)上遇到的错误是“ Enum Name此时无效”。 Am I doing something wrong, or can this not be done? 我是在做错什么,还是做不到?

MORE INFO 更多信息

I am trying to pass the Enum (Days), and randomly get back one of the values. 我试图传递枚举(天),并随机取回其中一个值。

NEVERMIND: I was over-complicating this part. 没关系:我把这部分复杂化了。

You're trying to pass a type name as if it were an argument value . 您正在尝试传递类型名称 ,就像它是参数值一样 You can't do that. 你不能那样做。 However, you can do: 但是,您可以执行以下操作:

[AttributeUsage(AttributeTargets.Property,AllowMultiple=true)]
public class EnumHelper : Attribute
{
    public Type EnumType;
    public EnumHelper(Type enumType)
    {
        EnumType = enumType;
    }
}

...

[EnumHelper(typeof(Days))]
public Days DayOfWeek { get; set; }

However: 然而:

  • I wouldn't personally make EnumType a public field; 我个人不会将EnumType为公共字段; make it a property. 使其成为财产。
  • There's currently no validation that EnumType is actually an enum. 当前没有验证EnumType 实际上是一个枚举。 You can't do it at compile-time, but you could do it at execution time. 您不能在编译时执行此操作,但是可以在执行时执行。
  • For the sake of convention, it should be called EnumHelperAttribute (or something more descriptive, really) - this isn't causing the error, but it's more idiomatic 为了方便起见,应将其称为EnumHelperAttribute (或更EnumHelperAttribute ,是更具描述性的)-这不会引起错误,但更常见
  • I'm not really sure I see the benefit... you can find the type of the property from the metadata already; 我不确定我是否会从中受益,您已经可以从元数据中找到属性的类型了。 what do you think the attribute is actually buying you? 您认为该属性实际上在购买您什么?

If you could let us know what you're trying to accomplish, we may be able to be more useful to you. 如果您可以让我们知道您要完成的工作,我们可能会对您有所帮助。

The parameters in Attributes can be only constants. 属性中的参数只能是常量。 If you want pass the enum type you must pass only the type: 如果要传递枚举类型,则必须仅传递类型:

[EnumHelper(typeof(Days))]
public Days DayOfWeek { get; set; }


[AttributeUsage(AttributeTargets.Property,AllowMultiple=true)]
public class EnumHelper : Attribute
{
    public Type MyEnum;
    public EnumHelper(Type enum)
    {
        MyEnum = enum;
    }
}

参数应该是枚举值,而不是枚举类型,例如:

[EnumHelper(Days.Sat)]

Just wanted to add how I ran into this and fixed it. 只是想补充一下我遇到的问题并加以解决。 I had my property named the same as my enumeration. 我的财产与枚举相同。 The code would compile and run, but I was getting a the red line error message in the IDE. 该代码将编译并运行,但是我在IDE中收到一条红线错误消息。 Changing the name of the property to something unique cleared the message. 将属性名称更改为唯一的名称将清除该消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM