Generic Enum Parsing and bit changing.

I was very frustrated the other day when I wanted to write a couple of generic functions in C# which take an enum generic parameter and turn flags on or off and check if there on etc. Just to tidy up lots of bitwise checking in my code.
Imagine my dissapointment when it got :

error CS0702: Constraint cannot be special class 'System.Enum'

This was quite iritating as I could seem to find out why. What I did find out though was that C++ for CLR did allow this, so I pulled out VC++ 2005 and had a go at doing what I wanted. I am no great C++ programmer so the thing that turned this from about 2 minutes work to 30 minutes was that I had declared the type and the methods without a public: block so I couldnt seem them in C#.

 
All thats fine now so I have got a class with some generics methods for making enum work simpler. I have kept the C++ for this as there are probably going to be other uses, especially with arrays (which get the same error). Heres the code for a C++ class library targeted to the CLR:
namespace BasicHelpers{

	public ref class EnumHelper  sealed : System::Object
	{
		public:
			generic  where T : System::Enum
			static T Parse(System::String ^valueName)
			{
				return Parse(valueName, false);
			}
	 
			generic  where T : System::Enum
			static T Parse(System::String ^valueName, bool ignoreCase)
			{
				return safe_cast(System::Enum::Parse(T::typeid, valueName, ignoreCase));
			}

			generic	 where T : System::Enum
			static System::Boolean IsFlagged(T value, T flag)
			{
				System::Int32 i1 = safe_cast(value );
				System::Int32 i2 = safe_cast(flag );
				return safe_cast((i1 & i2) == i2);
			}

			generic  where T : System::Enum
			static T Flag(T value, T flag, System::Boolean onOrOff)
			{
				System::Int32 i1 = safe_cast(value );
				System::Int32 i2 = safe_cast(flag );
				System::String ^name = System::Enum::GetName(T::typeid,(onOrOff ? (i1 | i2) : (i1 & ~i2)));
				return Parse(name);
			}
	
	};

};

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to Generic Enum Parsing and bit changing.

  1. Bill says:

    I have a helper class that is every similar to that (in c#) with some extra stuff in it. Take a look.
     
    http://rodenbaugh.net/archive/2006/11/01/Enum-Helper-Class-Using-Generics.aspx

  2. Ciaran says:

    Your class is pretty cool. It is possible to code all the same functionality in the C++ class I have, the only difference would be the ability of c++ to specify the where  T : Enum constraint.
    I was really just try so show people it could be done and to provide the most common usage.
     
    Thanks for taking the time to comment though. It makes it seems more worth while writing things if people actually take the time to care about it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s