Reflection
Reflection
is a powerful
programming feature supported by .Net. The name describes exactly what
it does
- it reflects.
With
reflection a program can
read about itself and perform certain tasks for itself or the user.
One
use of reflection is to
discover the members of a class and act on those members. Perhaps the
name of a
member gets passed to a function that needs to be evaluated.
I
ran across such a scenario
in a program that filled up an ArrayList of records. I needed to
perform some
filtering on that ArrayList that with SQL was a little too complicated.
I
needed to do some intersections and minus operations that might have
been
easier to do with Oracle than with MS SQL but, I also wanted to see how
it
would be if I created my own little filtering language.
What
I ended up using was
the “GetProperty” method from the Type class, then
use “GetValue” to find the
value of a class member.
Here
would be an example:
string
str1 = rectype.GetProperty(field).GetValue(rc1[i], null).ToString();
“field”
is just a string type containing the name of the member, and rc1 is a
collection
type containing an instance of a class having the members.
Although
this type of reflection has quite a performance penalty especially over
much
iteration, it at least gets the job done.
With a little innovation one can create a nice
flexible
filtering program. Reflection programming makes it happen.