Every once in a while you might want to know what fields, properties, or events a certain type of object contains at runtime. A common use for this information is serialization. .NET contains lots of different serialization techniques, like binary and XML, but sometimes you just have to roll your own. This tutorial is going to demonstrate how to get a list of public fields, properties, and events from objects at runtime.
First, let's create a simple object that contains some fields, properties, and events. I'll be using this object throughout the rest of the tutorial.
public class MyObject
{
//public fields
public string myStringField;
public int myIntField;
public MyObject myObjectField;
//public properties
public string MyStringProperty { get; set; }
public int MyIntProperty { get; set; }
public MyObject MyObjectProperty { get; set; }
//public events
public event EventHandler MyEvent1;
public event EventHandler MyEvent2;
}
The .NET class that gives us access to all of this is the Type class. To get a Type object, we simply use the typeof keyword:
Type myObjectType = typeof(MyObject);
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();
foreach (System.Reflection.FieldInfo info in fieldInfo)
Console.WriteLine(info.Name);
// Output:
// myStringField
// myIntField
// myObjectField
An important thing to note here is that the fields are not guaranteed to come out in any particular order. If you use GetFields, you should never depend on the order being consistent. 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 MyObject - that's where the real power comes in.
MyObject myObjectInstance = new MyObject();
foreach (System.Reflection.FieldInfo info in fieldInfo)
{
switch (info.Name)
{
case "myStringField":
info.SetValue(myObjectInstance, "string value");
break;
case "myIntField":
info.SetValue(myObjectInstance, 42);
break;
case "myObjectField":
info.SetValue(myObjectInstance, myObjectInstance);
break;
}
}
//read back the field information
foreach (System.Reflection.FieldInfo info in fieldInfo)
{
Console.WriteLine(info.Name + ": " +
info.GetValue(myObjectInstance).ToString());
}
// Output:
// myStringField: string value
// myIntField: 42
// myObjectField: MyObject
Combining this ability with the ability to create custom attributes provides a framework on which almost any serialization technique can be built.
Properties and events are retrieved almost identically to fields:
Type myObjectType = typeof(MyObject);
//Get public properties
System.Reflection.PropertyInfo[] propertyInfo =
myObjectType.GetProperties();
foreach (System.Reflection.PropertyInfo info in propertyInfo)
Console.WriteLine(info.Name);
// Output:
// MyStringProperty
// MyIntProperty
// MyObjectProperty
//Get events
System.Reflection.EventInfo[] eventInfo =
myObjectType.GetEvents();
foreach (System.Reflection.EventInfo info in eventInfo)
Console.WriteLine(info.Name);
// Output:
// MyEvent1
// MyEvent2
The PropertyInfo class is very similar to the FieldInfo class and also contains the ability to set the value of the property on an instance. It also gives you the ability to individually receive the get and set accessors as MethodInfo classes through the GetAccessors method.
An EventInfo object gives you lots of information about the event and the ability to add events to instances of MyObject.
I think that about does it. Hopefully this helps anyone out there wanting to get information about objects at runtime.
Thanks, this is the first, clear tutorial about reflection. Will solve some of my problems I had.
What a helpful and beautifully written page. No bullshit, no crappy code, just what I was looking for.
Thank you so much.
Can you get the name of the origional object through reflection as well? In you example you are getting information about members of the object you've been passed. I need to get the origional name of the object instance. In one of your first example this would be "myObjectInstance". Is this accessible at all?
The JIT doesn't really keep variable names around at runtime, so I don't believe it's possible.
How to find the list of classes available in .cs file using reflection......
Thanks for the explanation. its so clear
super tutorial....keep it up....
THANKSSSS!!!
is it possible to get the property of the property myObject?
great... simple and effective
how can we get summary information too?
Thanks!!!
nice example and well explained..thanks
This is just too beautiful. Thanks pal (sniff)
"the fields are not guaranteed to come out in any particular order"
Is there a way to get the correct order of the objects?
Since the order doesn't matter at compile-time or run-time, there is no concept of 'correct order'. If you mean the order in which they appear in your source code, then no, there's no way to do that.
I agree. Finally a Reflection article thats totally understandable and accesssible. Thank you! Very well written, and has given me enough knowledge to "take it from here". (after quite alot of googling)
Excellent Article. If it is hard to get the order of the field/property as it is declared in source code using GetFields or GetProperties, I got a question, how DataGridView class gets these field order details from source code, when binding the list of object to it.
thank you so much, this is a very helpful and well explained topic.
What is the name of the object??
Suppose i am writing Sample s = new Sample(), i am creating an object of sample class but i want to know what is the name of this object.
In your example, what is the name? If it's "s" then reflection cannot be used to retrieve that. What you name your variables has no impact on the definition or description of an object.
Thanks a lot
Very nice and absolutely and no blurred spots and clear to understand.