Introduction to Reflection API

FieldInfo

FieldInfo class discovers the attributes of a field and provides access to field metadata.

To get a list of public fields in an object, we'll use Type's GetFields method:

Type myObjectType = typeof(MyObject);

System.Reflection.FieldInfo[] fieldInfo = myObjectType.GetFields();

 

The FieldInfo class that gets returned actually contains a lot of useful information. It also contains the ability to set that field on an instance of object- that's where the real power comes in.

Other properties and methods you can check on MSDN

Example

Set instance field values using Reflection API
using System; class MyClass { public s...

 

Page 6 of 11