Link to home
Start Free TrialLog in
Avatar of Ricky White
Ricky WhiteFlag for United States of America

asked on

When to use .Net Reflection?

One of the features of using Reflection is that we can create code on the fly. I cannot imagine situations that would warrant creating code on the fly.

Can you please provide some sample scenarios?

Thanks.
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

One possible area when you generate code on a fly is when you allow you application users to write, for example, their own rules how to execute some operations. It may be some insurance company rules how to insure a car or a house, or may be some rules how to pack or store goods etc. Such rules can be written by by a user as a text using, of course, some scripting language (may be custom language). These rules are stored, e.g., in a database. When running your application these rules have to be interpreted and some code is to be generated and executed.
Couple of other examples:

The XMLSerializer generates code and compiles it on first run (http://www.hanselman.com/blog/HOWTODebugIntoANETXmlSerializerGeneratedAssembly.aspx).

Code is generated, for example, in report engines, such as Stimulsoft. It has to be run, of course.

Different unit test libraries, like RhinoMocs, use code generators to create mock objects.

Definitely, people may give more examples.
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I used reflection when I needed to allow users to effectivley build their own if statements by selecting variables and operators from drop down lists creating an If statement in plain english that the program will then use from it's settings.
Avatar of Ricky White

ASKER

Thanks!