Click here to Skip to main content
15,898,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone!

I am working on system.reflection in C#. I have to read the enums values using reflection.But while doing am not getting the values.

Can any help me how to do the process?

C#
private void ExtractSampleConstants(Type t)
    {
        FieldInfo[] FInfo = t.GetFields(BindingFlags.DeclaredOnly | BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
        foreach (FieldInfo fin in FInfo)
        {
            string ReplaceStr=string.Empty;
            string Fname = fin.Name;
            object Fvalue = fin.GetValue(fin.Name);
            if(t.Name=="SampleConstants")
                ReplaceStr=fin.DeclaringType.Name.ToString()+"."+Fname;
            else
                ReplaceStr = ConvertToShortForm(fin.DeclaringType.FullName.ToString()) + "." + Fname;
            DataDecl.FINFO.Add(new ConstInfo(Fname, Fvalue, ReplaceStr));
        }
Posted
Updated 26-Oct-10 0:13am
v2

If you must use reflection for this, then replace your line
object Fvalue = fin.GetValue(fin.Name);

with
C#
object Fvalue = fin.GetRawConstantValue();
 
Share this answer
 
Comments
hemantwithu 26-Oct-10 5:59am    
Thanks
You you mean the ordinal values?

All you have to do is cast it to the appropriate integer type.
 
Share this answer
 
You could use the static methods of the Enum class:

C#
public enum SomeEnum
{
  SomeItem,
  SomeItem2,
  SomeItem3
}

public void Something()
{
  var values = Enum.GetValues(typeof(SomeEnum));
}


and you'll not have to mess around with reflection.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900