How do I add multiple attributes to an Enum?
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; }
}
Related videos on Youtube
Comments
-
Code Maverick over 1 year
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.
-
Blorgbeard about 9 yearsWhat do you mean, "what I figured was wrong"?
-
BradWhy convert to an enum at all? Why not read from the table?
-
Code Maverick about 9 yearsexactly what I just did ... I can't mark yours as the answer because I got it working prior, but I will definitely +1
-
Code Maverick about 9 yearsoh what the heck ... I'll delete mine and give you the check =D