Reflection api

  • Thread starter Thread starter ichor
  • Start date Start date
I

ichor

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
 
Something I use it for is for "Addin" type support for applications.

The current project I am working on is a smart client application, which has pre-defined interfaces for addin modules
The application has a config file which specifies an assembly name that contains addin modules. Using reflection it is easy to examine the essemblies, find addins (types that support set interfaces) and then instantiate them, all while maintaining type safety.

Another useful function is for the basic datatypes (int, double, datetime etc).. They all have a pass method. Unfortunatly the pass method is not defined on an interfce or anything else like that, so using reflection, you can check a data type to see if it supports the parse method, and if it does you can then invoke it. Once again, this method allows you to retain type safety.

There are hunders of other uses including self desribing business objects, where properties have attibutes defining min/max value, required flags etc. Using reflection you can create "smart" controls that examine these attributes and automatically enforce the rules. It really helps to cut down on the amount of basic UI validation required

These are just some of the things I have used reflection for. I'm sure there are hundreds of other really nice uses.

Cheer

Eddie
 
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
 
hi

I am not clear about reflection apis uses.
what is the pass method?
can you give me a very basic example
thnx


Eddie said:
Something I use it for is for "Addin" type support for applications..

The current project I am working on is a smart client application, which
has pre-defined interfaces for addin modules.
The application has a config file which specifies an assembly name that
contains addin modules. Using reflection it is easy to examine the
essemblies, find addins (types that support set interfaces) and then
instantiate them, all while maintaining type safety..
Another useful function is for the basic datatypes (int, double, datetime
etc).. They all have a pass method. Unfortunatly the pass method is not
defined on an interfce or anything else like that, so using reflection, you
can check a data type to see if it supports the parse method, and if it does
you can then invoke it. Once again, this method allows you to retain type
safety..
There are hunders of other uses including self desribing business objects,
where properties have attibutes defining min/max value, required flags etc.
Using reflection you can create "smart" controls that examine these
attributes and automatically enforce the rules. It really helps to cut down
on the amount of basic UI validation required.
These are just some of the things I have used reflection for. I'm sure
there are hundreds of other really nice uses..
 
Back
Top