…meie igapäevast IT’d anna meile igapäev…

2008-08-04

Getting enum name in .NET Compact Framework

Filed under: Programmeerimine — Sander @ 09:44:00

(note: I really really hope that I am dead wrong with this. Please let me know if you know a better solution…)

Don’t ask me why, but for some reason the server side of the PDA application I am currently developing doesn’t return the HTTP/1.1 status code in a normal way. I guess that doing that would be just too easy, you know, as I would have HttpWebResponse.StatusCode and HttpWebResponse.StatusDescription in my disposal and that’s it.

No. It always returns 200 (OK) and actual HTTP status code in header. Don’t ask me why. People, who should be smarter and more experienced then I, developed this…

But I needed to get the status message – to display it to the user, so they would understand what is wrong, not only cryptic numbers. So, I thought – that isn’t a problem. There is an enum HttpStatusCode – I’ll just pull the matching name from there and show that, “Forbidden” is at least somewhat more informative then “403”.

Enums are easy and powerful in full .NET Framework. You have all these nice methods:

  Name Description
Public method Supported by the .NET Compact Framework CompareTo Compares this instance to a specified object and returns an indication of their relative values.
Public method Supported by the .NET Compact Framework Equals Overloaded. Overridden. Returns a value indicating whether this instance is equal to a specified object.
Public method Static Format Converts the specified value of a specified enumerated type to its equivalent string representation according to the specified format.
Public method Supported by the .NET Compact Framework GetHashCode Overridden. Returns the hash code for the value of this instance.
Public method Static GetName Retrieves the name of the constant in the specified enumeration that has the specified value.
Public method Static GetNames Retrieves an array of the names of the constants in a specified enumeration.
Public method Supported by the .NET Compact Framework GetType  Gets the Type of the current instance. (Inherited from Object.)
Public method Supported by the .NET Compact Framework GetTypeCode Returns the underlying TypeCode for this instance.
Public method Static Supported by the .NET Compact Framework GetUnderlyingType Returns the underlying type of the specified enumeration.
Public method Static GetValues Retrieves an array of the values of the constants in a specified enumeration.
Public method Static Supported by the .NET Compact Framework IsDefined Returns an indication whether a constant with a specified value exists in a specified enumeration.
Public method Static Supported by the .NET Compact Framework Parse Overloaded. Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.
Public method Static Supported by the .NET Compact Framework ReferenceEquals  Determines whether the specified Object instances are the same instance. (Inherited from Object.)
Public method Static Supported by the .NET Compact Framework ToObject Overloaded. Returns an instance of the specified enumeration type set to the specified value.
Public method Supported by the .NET Compact Framework ToString Overloaded. Overridden. Converts the value of this instance to its equivalent string representation.

But as you look at the table, you’ll see that not all methods are available for Compact Framework… and if you look further, you’ll notice that out of all actually useful Enum methods, only Enum.Parse() is supported on CF. I understand that CF needs to be tiny, but could you at least leave us couple of badly needed things?

So, long story short, I ended up using System.Reflection to get the name of matching HTTP status code. I will put the code here in hopes that I nor anyone else will never need it – neither for getting HTTP status message nor name from any enum.

using System.Reflection;
...
 
Type enumType = typeof(HttpStatusCode);
FieldInfo[] fieldInfo = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
                  
int iCode = int.Parse(rslt.Headers.Get("ResponseCode"));
string sMessage = string.Empty;
 
for (int i = 0; i < fieldInfo.Length; i++)
{                        
    if ((Int32)fieldInfo[i].GetValue(null) == iCode)
    {
        sMessage = fieldInfo[i].Name;
    }
}

Lisa kommentaar »

Kommentaare veel pole.

RSS feed for comments on this post. TrackBack URI

Lisa kommentaar

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 / Muuda )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Blog at WordPress.com.