There are many applications. Reflection is a powerful instrument, for
example if you want to write generic software that handles objects that
are not known during design time (e.g. an exception class, that logs all
values of a class).
I take my .NET Data Access Objects as an example (it's a persistence
framework). The Mapping Unit inspects classes via refelction to
determine its fields. Besides that, it caches mapping information to map
column values of a data row to the fields. This enables to initialize
arbitrary objects with a data retrieved from a database.
Simplified example:
//write data to each mapped field of the class
foreach (FieldMapping mapping in classMappings)
{
//FieldInfo is a pointer to a field of a class (reflection)
FieldInfo fi = mapping.FieldInfo;
//the name of the column in a DataRow (retrieved data)
ColumnName col = mappings.ColumnName;
//retrieve column value and write it to the field
//value of the instance
fi.SetValue(myCurrentInstance, dataRow[col]);
}
Cheers, Philipp
ichor wrote:
hi i have read a few examples that use the
System.Reflection api , but i cant understand in what way is it helpful. i
mean its practical use in the real world
thanx