Reflecting All of The Members of a Class | | Reflection is one of the new features of .NET and it's way cool. In a nutshell, Reflection allows you to interrogate an object, your's or someone elses, and find out all about it at runtime. Let me show you a really simple illustration. Let's say that I have an object called Broadcaster (I made a Broadcaster object for another article, so I was just being object-oriented (or lazy depending who you ask) ) and you want to see all of its members...this just a few lines of code will get you there:
In VB.NET Type:
Dim t As Type = c.GetType
Dim ObjectMembers As MemberInfo() = t.GetMembers
Dim Iterator As MemberInfo
For Each Iterator In ObjectMembers
Debug.WriteLine(Iterator.Name)
Debug.WriteLine(Iterator.ReflectedType.ToString)
Next |
In C#
BroadCaster bc = new BroadCaster();
Type t = typeof(c);
MemberInfo[] ObjectMembers = t.GetMembers();
foreach(MemberInfo m in ObjectMembers)
{
Debug.WriteLine(m.Name);
Debug.WriteLine(m.ReflectedType.ToString());
} |
The Output of what you'll find is provided below:

What you see is quite detailed, all of the methods, properties (even ToString which is an inherited method), delegates, yep, just about everything. You can do quite a bit more with Reflection, this is about as elementary as it gets, but it should give you a decent feel for the types of things that can be done with it.
|