Thread: Reflection

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Posts
    24
    Rep Power
    0

    Reflection


    Hello,

    I have an enum defined as follows

    public enum EnumStuff {
    Value1 = 1,
    Value2 = 2,
    Value3 = 3
    }

    I am trying to retrieve the name as well as the value from each value inside of this enum. However, I have tried the following and it does not work.

    public class Test {
    public static void Main(String[] args) {
    EnumStuff es = EnumStuff.Value1
    Type t = e.GetType();
    FieldInfo[] fi = t.GetFields();

    foreach (FieldInfo i in fi) {
    Console.WriteLine("Name: " + i.Name);
    Console.WriteLine("Value: " + i.GetValue(es));
    }
    }
    }

    Instead of printing the value, it prints the name of the variable. Is there a way to display both the name and the value for each variable in the enum?

    Thank you for your help
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Posts
    24
    Rep Power
    0

    solved


    solved. Thanks for viewing

IMN logo majestic logo threadwatch logo seochat tools logo