Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

[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". 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.

like image 288
Martin Avatar asked Sep 15 '09 18:09

Martin


People also ask

Can enums have attributes?

An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden).

Why is enum not good?

They Lack Accountability. To be clear, it's a mistake to assume consensual non monogamy is void of commitment. It's not simply informing your casual hookup of your other casual hookups. For a lot of people, it's being communicative to their partner of what they're doing with their friend with benefits or boyfriend.

Can you cast an enum to an int?

Overview. To convert an enum to int , we can: Typecast the enum value to int . Use the ToInt32 method of the Convert class.

Can we use enum as array?

Enums are value types (usually Int32). Like any integer value, you can access an array with their values. Enum values are ordered starting with zero, based on their textual order. MessageType We see the MessageType enum, which is a series of int values you can access with strongly-typed named constants.


2 Answers

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; make it a property.
  • There's currently no validation that EnumType is actually an enum. 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
  • 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.

like image 182
Jon Skeet Avatar answered Sep 27 '22 20:09

Jon Skeet


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;
    }
}
like image 27
TcKs Avatar answered Sep 27 '22 19:09

TcKs