Reflection is the process by which a computer program can observe and modify its own structure and behavior. Assemblies contain modules, modules contain types, and types contain members. Reflection provides objects that encapsulate assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object. You can then invoke the type's methods or access its fields and properties. Typical uses of reflection are details here.
Getting members of an Enum via reflection
It’s very simple to get the value when enum value taken from same class. In this code enum value has to provide for getting type and the type object can supply the field with GetFields() method. Here the GetFields() method works for reflection.
Look at this example,
static void Main(string args)
Type enumType = typeof(EnumName);
Infos = enumType.GetFields();
infos = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
foreach (FieldInfo fi in infos)
But what should we do when the enum accessed from different class? In that case we have to use reflection. Because reflection can provides method, constructor, type of another class or assembly.
My main purpose is not to get the value from same class, I will show how to get enum value from another class and with dynamic class object. I mention dynamic because I can choose class any class and takes the object then getting the enum value through the class object. OK, let’s start the discussion how the code is working. GetMembers() is another reflection method which returns the public or static method as MemberInfo.
Suppose I have a class as ClassOne then I can get all the methods using the reflection method.
public class ClassOne
public enum EnumName1
OneValue = 1,
TwoValue = 2,
ThreeValue = 3,
FourValue = 4
public enum EnumName2
FiveValue = 500,
SixValue = 600,
SevenValue = 700,
EightValue = 800
In that case If I want the enum names then:
Object dynamicObject=new ClassOne();
Type dynamicType = dynamicObject.GetType();
MemberInfo memberInfos = dynamicType.GetMembers(BindingFlags.Public | BindingFlags.Static);
I could use the class name directly for getting type but here I used to show how to get the type in dynamic selection.
Now, all the methods are now in memberInfos variable. So all the enum name can find if foreach use like this:
foreach (MemberInfo t in memberInfos)
But I needs the values of enum then again I need to use reflection and get the type of enum but problem is I have to get the type from Enum name. But its not possible to get type using GetType() method with enum name only. For that I had to use the Namespace, class then enum name like this format:
var typeEnums = System.Type.GetType(“Namespace Name” + "." + “Class Name” + "+" + “Enum Name”);
Now it’s very easy to get the enum value’s from typeEnums using GetFields() method.
var fieldsArray = typeEnums.GetFields(BindingFlags.Public | BindingFlags.Static);
foreach (var fInfo in fieldsArray)
var ulngValue = (ulong)Convert.ChangeType(fInfo.GetValue(null), typeof(ulong));
Console.WriteLine(fInfo.Name.ToString(CultureInfo.InvariantCulture) + ":" + ulngValue.ToStr ing(CultureInfo.InvariantCulture));
At last my desired output comes:
OneValue : 1
TwoValue : 2
ThreeValue : 3
FourValue : 4
FiveValue : 500
SixValue : 600
SevenValue : 700
EightValue : 800