[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 572
  • Last Modified:

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?

1 Solution
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.
Éric MoreauSenior .Net ConsultantCommented:
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.
Ricky66Author Commented:

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now