David Smith

David Smith

  • NA
  • 2k
  • 0

Description Attr

Oct 16 2010 12:02 AM
  it blows up at this line when i enter in 00AA or 0x00AA, i guess its not really possible to put a enumeraton out of  order
                                                                                                               

string value = commonFunctions.GetTestName((Dictionary.Common.ValidtTest)Enum.Parse(typeof(Dictionary.Common.ValidTest), "00AA"));    

   public enum ValidTest
    {
      [Description("TEST_A")] CAT = 0x0000,
      [Description("TEST_B")] DOG = 0x00AA,
      [Description("TEST_C")] BEAR = 0x0002,  
    }


    public string GetTestName(Enum eventValue)
    {
      ValidTest vtn = (ValidTest)eventValue;

      // Get the type
      Type type = eventValue.GetType();

      // Get fieldinfo for this type
      FieldInfo fieldInfo = type.GetField(vtn.ToString());


      // Get the stringvalue attributes
      DescriptionAttribute[] attribs = fieldInfo.GetCustomAttributes(
          typeof(DescriptionAttribute), false) as DescriptionAttribute[];      
                                                                                                            

      // Return the first if there was a match.
      return attribs.Length > 0 ? attribs[0].Description : null;
    }

Answers (2)