Reflection - When to use?

  • Thread starter Thread starter heddy
  • Start date Start date
H

heddy

I understand that reflection allows me to discover the metadata of a
class at runtime (properties, methods etc). What I don't understand is
where this is useful. For example:

If I am the sole author of an application, I already know the metadata
about my classes - I wrote them. And even if I did not - Let's say I
buy a 3rd party class lib, it will be documented for me with that lib.

If I am part of a dev team, the same is true - If I use a class that I
did not author, I should still be able to reference the documentation
for that class and use it.

Can someone clarify for me?
 
"heddy" <[email protected]> a écrit dans le message de (e-mail address removed)...

|I understand that reflection allows me to discover the metadata of a
| class at runtime (properties, methods etc). What I don't understand is
| where this is useful. For example:
|
| If I am the sole author of an application, I already know the metadata
| about my classes - I wrote them. And even if I did not - Let's say I
| buy a 3rd party class lib, it will be documented for me with that lib.
|
| If I am part of a dev team, the same is true - If I use a class that I
| did not author, I should still be able to reference the documentation
| for that class and use it.
|
| Can someone clarify for me?

For example, I write many different business classes, and I need to store
instance of these classes in a mechanism that hides the database and
translates those objects into SQL commands.

I write a storage mechanism with methods like this :

class Storage
{
void StoreObject(object obj)
{
PropertyInfo[] properties = obj.GetType().GetProperties();
// read properties and their values
// and create SQL Update or Insert statements
// using the names and values found.
}
...
}

and many other situations where the type of an object could be one of many.

However, reflection is not always necessary for these kind of tasks as the
Visitor design pattern can do this kind of thing

Joanna
 
If I am the sole author of an application, I already know the metadata
about my classes - I wrote them. And even if I did not - Let's say I
buy a 3rd party class lib, it will be documented for me with that lib.

If that's all you have, you won't likely need to deal with reflection in
your scenario.

Here's a common scenario. What if you decided that you wanted to open up
your application to others by offering a plug-in architecture. Your
application then loads in these third party plug-ins at runtime and executes
functions that you previously defined. Reflection would be used to evaluate
the type and member definitions of these plug-ins to determine compatibility
and to invoke the appropriate members at the appropriate times.

Another common scenario is manual serialization. Let's say that you wanted
to provide a class library that could take someone else's objects, serialize
them, transport them to another computer, and then deserialize them, even if
the objects were not marked as serializeable. You could use reflection to
deconstruct these objects, capture the values using preevaluated type
information that was understood using reflection, then reconstruct the
objects, possibly using System.Reflection.Emit.

Generally, .NET reflection is a handy feature that allows you to introspect
the assembly, type, and member information of any .NET solution (including
your own), or to construct new classes at runtime using Emit. However, you
asked a good question in that you should not care about reflection unless
you need it. It should never be used for loose typing, for example, when
strongly typed solutions can be accomplished just as easily, because
strongly typed solutions have better performance and are typically far more
stable and secure.

Jon
 
A great example is when you want to dynamically discover
something about a class. I've written methods that permit
classes to self validate required fields. Then, have all
by data classes inherit this base class with these methods.

I've written classes that can dynamically copy themselves
to "similar" classes that have slightly different signatures.
Great for unit tests and for windows forms to web service
type implementations.

I've used a similar approach to auto load a class at runtime
with results from a datatable:

http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp

As others have stated, you want to be aware of the performance
penalty. Typically, you avoid this by caching the reflected
results or creating static objects of them. Things like PropertyInfo[]
or FieldInfo[]. So, you perform the reflection once during the life
of your app on a given class. From that point forward, your
app will perform at 99.9% of the speed had you not used
reflection at all. And saved, yourself a ton of typing and
reduced the risk of runtime bugs when things in the database
change.

It can be a huge benefit if you use it wisely.
 
Hi,


heddy said:
I understand that reflection allows me to discover the metadata of a
class at runtime (properties, methods etc). What I don't understand is
where this is useful. For example:

Can someone clarify for me?

I thikn that the best example is Binding , when you bind a collection of
objects (or even a strong typed dataset) to a grid for example it use
reflection to know about the properties of the objects and create the
columns.

Another good example is a generic sorter class, see my post in the "User
class sorting" thread. basically you have a class that implement IComparer
that accept as parameter the property name over which it will sort a
collection.


In general reflection is used a lot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top