Matching enumeration with reflection in Compact Framework

I am trying to match an enumeration defined inside a class that is instantiated as an object with an Activator in the Compact Framework.

Code Snippet:

public class MyClass
{
public enum EnumeratedType : short
{
// Requests
RequestA = 0x01,
RequestB = 0x02,
// Responses
ResponseA = 0x11,
ResponseB = 0x12,
};
}

The object is instantiated thus:

Type t = Type.GetType( "MyClass+EnumeratedType" );
object ob = Activator.CreateInstance( t );

If ob is then set to, say, 0x11 by another operation, I need to access the string of the enumerated name, but so far I have had no luck.

With Reflection I can access the enumerated types in a FieldInfo array, which gives me an array of all the possible names, but there doesn't seem to be a value associated with these - which would make it easy to match and extract the name.

All I need something like:

string name = ob.EnumQualifiedName();

Any suggestions would be appreciated!



Answer this question

Matching enumeration with reflection in Compact Framework

  • Guglielmo

    Thanks for the reply.

    Unfortunately System.Enum.GetName is not supported in the Compact Framework.


  • Kristin

    Use the System.Enum.GetName(Type type, Object value) static method.
  • wardw

    For value -> name direction its very easy to get around using Enum.GetName:

    class Program

    {

    enum EnumeratedType : short

    {

    // Requests

    RequestA = 0x01,

    RequestB = 0x02,

    // Responses

    ResponseA = 0x11,

    ResponseB = 0x12,

    }

    static void Main(string[] args)

    {

    Debug.Assert( "ResponseA" == GetName(typeof(EnumeratedType), 0x11));

    }

    static string GetName(Type enumType, object value)

    {

    return Enum.ToObject(enumType, value).ToString();

    }

    }

    In the name -> value direction Enum.Parse will do what you need, but that one does require NETCFv2 I believe.

    -Noah

    .Net Compact Framework


  • martinhughes

    That is exactly what I am looking for... many thanks!

    I had implemented a temporary workaround using a custom attribute on each enumeration that contained its value, but the extraction code had to iterate through a FieldInfo array every time.

    This is perfect!


  • Tally622

    Oh, sorry. I didn't realize. They support fullblown reflection, but not a simple method like Enum.GetName Thats crazy...

    Anyway, from your original post, are you saying that FieldInfo.GetValue is returning 0 or null for the fields that belong to the enum


  • Jeff Lewis

    Hi,

    Yes the compact framework is a little bizzare in what is implemented - and particularly what is not!

    What I am saying is that if my object is set to a value, there seems to be no way to retrieve a string of the enumerated name associated with that value.

    The FieldInfo array contains all of the enumerated types, and a GetValue on each type does indeed return a string of the name. However, I can't find a value associated with a particilar FieldInfo array element.

    For Example, a GetValue on the FieldInfo for ResponseA returns its name, but there appears to be nothing to say that name is enumerated to 0x11.


  • Matching enumeration with reflection in Compact Framework