Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add multiple attributes to an Enum?

I have a SQL lookup-table called ClientCreditResolutionPlanActionType that I want to convert to an enum in c#.

Very basic request, right? Right.

My table, now enum, however, has several columns, or now, description properties that need to go with it:

  • StatusIcon
  • StatusText
  • TypeText

So I figured I could do ...

namespace System.ComponentModel
{
    class StatusIconAttribute : Attribute
    {
        public string StatusIcon;
        public StatusIconAttribute(string statusIcon) { StatusIcon = statusIcon; }
    }

    class StatusTextAttribute : Attribute
    {
        public string StatusText;
        public StatusTextAttribute(string statusText) { StatusText = statusText; }
    }

    class TypeTextAttribute : Attribute
    {
        public string TypeText;
        public TypeTextAttribute(string typeText) { TypeText = typeText; }
    }
}

... in my Extensions.cs class ...

public static class EnumExtensions
{
    public static string GetStatusIcon(this Enum value)
    {
        var type = value.GetType();

        string name = Enum.GetName(type, value);              
        if (name == null) { return null; }

        var field = type.GetField(name);
        if (field == null) { return null; }

        var attr = Attribute.GetCustomAttribute(field, typeof(StatusIconAttribute)) as StatusIconAttribute;
        if (attr == null) { return null; }

        return attr.StatusIcon;
    }

    public static string GetStatusText(this Enum value)
    {
        var type = value.GetType();

        string name = Enum.GetName(type, value);              
        if (name == null) { return null; }

        var field = type.GetField(name);
        if (field == null) { return null; }

        var attr = Attribute.GetCustomAttribute(field, typeof(StatusTextAttribute)) as StatusTextAttribute;
        if (attr == null) { return null; }

        return attr.StatusText;
    }

    public static string GetTypeText(this Enum value)
    {
        var type = value.GetType();
        string name = Enum.GetName(type, value);              

        var type = value.GetType();

        string name = Enum.GetName(type, value);              
        if (name == null) { return null; }

        var field = type.GetField(name);
        if (field == null) { return null; }

        var attr = Attribute.GetCustomAttribute(field, typeof(TypeTextAttribute)) as TypeTextAttribute;
       if (attr == null) { return null; }

        return attr.TypeText;
    }
}

... and finally in my other project use it like:

namespace ClientSystemServiceLibrary.Enums
{
    [DataContract]
    public enum ClientCreditResolutionPlanActionType
    {
        [EnumMember]
        [TypeText("New resolution plan submitted.")]
        [StatusText("New Plan")]
        [StatusIcon("star.png")]
        NewPlan = 1,

        [EnumMember]
        [TypeText("Resolution plan waiting on approval.")]
        [StatusText("Under Review")]
        [StatusIcon("review.png")]
        UnderReview = 2,

        [EnumMember]
        [TypeText("Resolution plan approved.")]
        [StatusText("Approved")]
        [StatusIcon("check.png")]
        Approved = 3,

        [EnumMember]
        [TypeText("Resolution plan rejected.")]
        [StatusText("Rejected")]
        [StatusIcon("cross.png")]
        Rejected = 4,

        [EnumMember]
        [TypeText("New resolution plan comment submitted.")]
        [StatusText("New Comment")]
        [StatusIcon("message.png")]
        NewComment = 5
    }
}E

Except, what I figured was wrong, as I'm receiving these error messages:

'System.CompenentModel.TypeTextAttribute' is inaccessible due to its protection level

and

The type or namespace name 'TypeText' could not be found (are you missing a using directive or an assembly reference?)

Same ... for all 3.

like image 681
Code Maverick Avatar asked Feb 24 '15 23:02

Code Maverick


People also ask

How do I assign multiple values to an enum?

Create enum constructor which accepts multiple values. Assign each constructor argument to a member field in the enum definition. Create getter methods so we can access any of the values assigned to a particular enum constant.

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).

Can enum have two values?

The Enum constructor can accept multiple values.

Can you add values to enum?

You can add a new value to a column of data type enum using ALTER MODIFY command. If you want the existing value of enum, then you need to manually write the existing enum value at the time of adding a new value to column of data type enum.


1 Answers

By default, all classes are internal. You should specify "public" access modifier, if you want them to be accessible from other assemblies. Like this:

public class TypeTextAttribute : Attribute
{
    public string TypeText;
    public TypeTextAttribute(string typeText) { TypeText = typeText; }
}
like image 119
alex Avatar answered Oct 10 '22 06:10

alex