KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have the following enumeration: public enum AuthenticationMethod { FORMS = 1, WINDOWSAUTHENTICATION = 2, SINGLESIGNON = 3 } The problem however is that I need the word "FORMS" when I ask for AuthenticationMethod.FORMS and not the id 1. I have found the following solution for this problem ( link ): First I need to create a custom attribute called "StringValue": public class StringValue : System.Attribute { private readonly string _value; public StringValue(string value) { _value = value; } public string Value { get { return _value; } } } Then I can add this attribute to my enumeration: public enum AuthenticationMethod { [StringValue("FORMS")] FORMS = 1, [StringValue("WINDOWS")] WINDOWSAUTHENTICATION = 2, [StringValue("SSO")] SINGLESIGNON = 3 } And of course I need something to retrieve that StringValue: public static class StringEnum { public static string GetStringValue(Enum value) { string output = null; Type type = value.GetType(); //Check first in our cached results... //Look for our 'StringValueAttribute' //in the field's custom attributes FieldInfo fi = type.GetField(value.ToString()); StringValue[] attrs = fi.GetCustomAttributes(typeof(StringValue), false) as StringValue[]; if (attrs.Length > 0) { output = attrs[0].Value; } return output; } } Good now I've got the tools to get a string value for an enumeration. I can then use it like this: string valueOfAuthenticationMethod = StringEnum.GetStringValue(Authenticat
Tags (comma-separated)
Save Edits
Cancel