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.
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).
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.
Overview. To convert an enum to int , we can: Typecast the enum value to int . Use the ToInt32 method of the Convert class.
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.
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:
EnumType
a public field; make it a property.EnumType
is actually an enum. You can't do it at compile-time, but you could do it at execution time.EnumHelperAttribute
(or something more descriptive, really) - this isn't causing the error, but it's more idiomaticIf 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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With