When to use .Net Reflection?

Posted on 2012-08-22
Last Modified: 2012-08-23
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?

Question by:Ricky66
    LVL 29

    Expert Comment

    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.
    LVL 29

    Expert Comment

    Couple of other examples:

    The XMLSerializer generates code and compiles it on first run (

    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.
    LVL 69

    Accepted Solution

    LVL 7

    Expert Comment

    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.

    Author Closing Comment


    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now