Scott Baldridge
asked on
String Enum
Hello, I have an enum like so:
public enum PermissionType
{
[EnumValueName("Paid Customer")]
PaidCustomer
}
If I have "Paid Customer" how do I get PaidCustomer?
public enum PermissionType
{
[EnumValueName("Paid Customer")]
PaidCustomer
}
If I have "Paid Customer" how do I get PaidCustomer?
You can surely use Enum.Parse. Check https://msdn.microsoft.com/en-us/library/essfb559(v=vs.110).aspx
Does ToString() not work?
What is EnumValueName? This is not something that is documented, so I do not know how to use it to get the reference.
But assuming a variable named permission that has the proper value:
permission.ToString()
But assuming a variable named permission that has the proper value:
permission.ToString()
Using reflection:
using System;
namespace EE_Q28869700
{
class Program
{
static void Main(string[] args)
{
var value = "Paid Customer".GetValueFromEnumValueName<PermissionType>();
Console.WriteLine(value);
Console.ReadLine();
}
}
enum PermissionType
{
[EnumValueName("Paid Customer")]
PaidCustomer
}
class EnumValueNameAttribute : Attribute
{
public string Name { get; set; }
public EnumValueNameAttribute() { ;}
public EnumValueNameAttribute(string name)
{
Name = name;
}
}
static class Extensions
{
public static T GetValueFromEnumValueName<T>(this string name)
{
var type = typeof(T);
if (!type.IsEnum)
throw new InvalidOperationException();
foreach (var field in type.GetFields())
{
var attribute = Attribute.GetCustomAttribute(field, typeof(EnumValueNameAttribute)) as EnumValueNameAttribute;
if (attribute != null)
{
if (attribute.Name == name)
return (T)field.GetValue(null);
}
else
{
if (field.Name == name)
return (T)field.GetValue(null);
}
}
throw new ArgumentException("Not found.", "name");
}
}
}
Which produces the following output --saige-
A more *non-intuitive* selection:
using System;
using System.ComponentModel;
namespace EE_Q28869700
{
class Program
{
static void Main(string[] args)
{
var value = "Paid Customer".GetValueFromEnumValueName<PermissionType>();
Console.WriteLine(value);
value = "Egads".GetValueFromEnumValueName<PermissionType>();
Console.WriteLine(value);
Console.ReadLine();
}
}
enum PermissionType
{
[EnumValueName("Paid Customer")]
PaidCustomer,
[EnumValueName("Egads")]
UnpaidCustomer
}
class EnumValueNameAttribute : Attribute
{
public string Name { get; set; }
public EnumValueNameAttribute() { ;}
public EnumValueNameAttribute(string name)
{
Name = name;
}
}
static class Extensions
{
public static T GetValueFromEnumValueName<T>(this string name)
{
var type = typeof(T);
if (!type.IsEnum)
throw new InvalidOperationException();
foreach (var field in type.GetFields())
{
var attribute = Attribute.GetCustomAttribute(field, typeof(EnumValueNameAttribute)) as EnumValueNameAttribute;
if (attribute != null)
{
if (attribute.Name == name)
return (T)field.GetValue(null);
}
else
{
if (field.Name == name)
return (T)field.GetValue(null);
}
}
throw new ArgumentException("Not found.", "name");
}
}
}
Which now produces the following output --saige-
ASKER
I tried to get PaidCustomer like this :
string permissionName = Roles.GetPermissionName(pe rmissionId );
//permissionName = "Paid Customer"
PermissionType permission = (PermissionType) Enum.Parse(typeof(Permissi onType), permissionName);
but get this error: Requested value 'Paid Customer' was not found.
string permissionName = Roles.GetPermissionName(pe
//permissionName = "Paid Customer"
PermissionType permission = (PermissionType) Enum.Parse(typeof(Permissi
but get this error: Requested value 'Paid Customer' was not found.
Correct, Parse will only work if the string matches the enum value. This is where the extension method plays it's role in order to match the string value with the EnumValueName attribute.
-saige-
-saige-
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you! I'm going to give it a go!
@it_saige
Where did you get the documentation for the EnumValueName attribute? I cannot find it anywhere, even by googling it.
Where did you get the documentation for the EnumValueName attribute? I cannot find it anywhere, even by googling it.
ASKER
WOW! that is the way to go! Works perfectly!
I just assumed it was a custom attribute since it was enclosed in Attribute declarative markup. From there, the attributes constructor accepts a string so I just assumed that the string's property was Name since the attributes name was EnumValueName.
-saige-
-saige-
Glad that worked out for you Earnie.
-saige-
-saige-
@Jacques Bourgeois... I believe that it is a custom attribute. I think i remember a project online somewhere..... if i find it i will post.
i know i shouldn't post a link here but...
http://www.codeproject.com/Articles/11130/String-Enumerations-in-C
http://www.codeproject.com/Articles/11130/String-Enumerations-in-C