Link to home
Start Free TrialLog in
Avatar of Scott Baldridge
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?
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Avatar of p_davis
p_davis

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()
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");
		}
	}
}

Open in new window

Which produces the following output -User generated image-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");
		}
	}
}

Open in new window

Which now produces the following output -User generated image-saige-
Avatar of Scott Baldridge

ASKER

I tried to get PaidCustomer like this :

string permissionName = Roles.GetPermissionName(permissionId);
//permissionName = "Paid Customer"

 PermissionType permission =  (PermissionType) Enum.Parse(typeof(PermissionType), permissionName);

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-
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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-
Glad that worked out for you Earnie.

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