Hmm...I don't think I did?
In reading my post again, it didn't seem very clear. For the heck of it, let me try again with a more clear head.
C# allows you to define properties for classes, unlike Java. What's a property? Try this.
class Employee
{
private string m_name = "";
public string Name
{
get { return m_name; }
set { m_name = value; }
}
}
If I were to create an Employee object:
Employee emp = new Employee();
...I could now easily get/set the value:
emp.Name = "Typedef Enum"; // Set the value
Console.WriteLine("Employee name: " + emp.Name); // Get the value
OK. If I wanted to use reflection to, say, query an object of type Employee:
System.Type t = emp.GetType();
Likewise, if I didn't have an employee object instanciated, I could also achieve the same thing with the following:
System.Type t = typeof(Employee);
Either way, I now have a type object. With a type object, you can now use reflection in a variety of ways to discover "things" about the type, such as:
- Is it an interface?
- Is it an abstract class?
- What are the property(s) names?
- What are the member function names?
- What are the fields?
etc. etc.
Using the example I gave before, I could do something like this.
(Note: Calling the GetProperties() function on class Type will return an array of PropertyInfo objects)
foreach (PropertyInfo pi in t.GetProperties())
{
Console.WriteLine("This property name is: " + pi.Name);
}
If I were to execute that code, it would spit out:
"This property name is: Name"
It's a very powerful feature, and one that's used throughout .NET (in particular), as well as Java. In fact, .NET Studio 2003 uses reflection a _lot_ in order to accomplish a lot of "things" within the IDE itself. It's also used quite a bit once you start using ADO.NET, especially once you begin the process of binding Control objects/classes with Data objects. That is to say, it's used to link pure data classes (collections, DataSets, tables, databases, etc) and UI components that display the data, even when the UI components have no preconceived knowledge about the structure of the data. The prime example is the DataGrid.
The DataGrid UI component is able to present data in a table, with column headers/etc. because it uses reflection to "inspect" the data structures to pull out this information.