Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 588653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:20:28+00:00 2026-05-13T15:20:28+00:00

I come from low level languages – C++ is the highest level I program

  • 0

I come from low level languages – C++ is the highest level I program in.

Recently I came across Reflection, and I just cannot fathom how it could be used without code smells.

The idea of inspecting a class/method/function during runtime, in my opinion, points to a flaw in design – I think most problems Reflection (tries to) solve could be used with either Polymorphism or proper use of inheritance.

Am I wrong? Do I misunderstand the concept and utility of Reflection?

I am looking for a good explanation of when to utilize Reflection where other solutions will fail or be too cumbersome to implement as well as when NOT to use it.

Please enlighten this low-level lubber.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:20:28+00:00Added an answer on May 13, 2026 at 3:20 pm

    Reflection is most commonly used to circumvent the static type system, however it also has some interesting use cases:

    Let’s write an ORM!

    If you’re familiar with NHibernate or most other ORMs, you write classes which map to tables in your database, something like this:

    // used to hook into the ORMs innards
    public class ActiveRecordBase
    {
        public void Save();
    }
    
    public class User : ActiveRecordBase
    {
        public int ID { get; set; }
        public string UserName { get; set; }
        // ...   
    }
    

    How do you think the Save() method is written? Well, in most ORMs, the Save method doesn’t know what fields are in derived classes, but it can access them using reflection.

    Its wholly possible to have the same functionality in a type-safe manner, simply by requiring a user to override a method to copy fields into a datarow object, but that would result in lots of boilerplate code and bloat.

    Stubs!

    Rhino Mocks is a mocking framework. You pass an interface type into a method, and behind the scenes the framework will dynamically construct and instantiate a mock object implementing the interface.

    Sure, a programmer could write the boilerplate code for the mock object by hand, but why would she want to if the framework will do it for her?

    Metadata!

    We can decorate methods with attributes (metadata), which can serve a variety of purposes:

    [FilePermission(Context.AllAccess)]    // writes things to a file
    [Logging(LogMethod.None)]              // logger doesn't log this method
    [MethodAccessSecurity(Role="Admin")]   // user must be in "Admin" group to invoke method
    [Validation(ValidationType.NotNull, "reportName")] // throws exception if reportName is null
    public void RunDailyReports(string reportName) { ... }
    

    You need to reflect over the method to inspect the attributes. Most AOP frameworks for .NET use attributes for policy injection.

    Sure, you can write the same sort of code inline, but this style is more declarative.

    Let’s make a dependency framework!

    Many IoC containers require some degree of reflection to run properly. For example:

    public class FileValidator
    {
        public FileValidator(ILogger logger) { ... }
    }
    
    // client code
    var validator = IoC.Resolve<FileValidator>();
    

    Our IoC container will instantiate a file validator and pass an appropriate implementation of ILogger into the constructor. Which implementation? That depends on how its implemented.

    Let’s say that I gave the name of the assembly and class in a configuration file. The language needs to read name of the class as a string and use reflection to instantiate it.

    Unless we know the implementation at compile time, there is no type-safe way to instantiate a class based on its name.

    Late Binding / Duck Typing

    There are all kinds of reasons why you’d want to read the properties of an object at runtime. I’d pick logging as the simplest use case — let say you were writing a logger which accepts any object and spits out all of its properties to a file.

    public static void Log(string msg, object state) { ... }
    

    You could override the Log method for all possible static types, or you could just use reflection to read the properties instead.

    Some languages like OCaml and Scala support statically-checked duck-typing (called structural typing), but sometimes you just don’t have compile-time knowledge of an objects interface.

    Or as Java programmers know, sometimes the type system will get your way and require you to write all kinds of boilerplate code. There’s a well-known article which describes how many design patterns are simplified with dynamic typing.

    Occasionally circumventing the type system allows you to refactor your code down much further than is possible with static types, resulting in a little bit cleaner code (preferably hidden behind a programmer friendly API 🙂 ). Many modern static languages are adopting the golden rule “static typing where possible, dynamic typing where necessary”, allowing users to switch between static and dynamic code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 302k
  • Answers 302k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The password is used to protect the integrity of a… May 13, 2026 at 8:24 pm
  • Editorial Team
    Editorial Team added an answer Yes, you can't make this kind of code work in… May 13, 2026 at 8:24 pm
  • Editorial Team
    Editorial Team added an answer Are you sure it has nothing to do with the… May 13, 2026 at 8:24 pm

Related Questions

Sorry for the semi-rant here. I am hooked on javascript and want some direction
I'm about to undertake updating our coding standards documentation and I am thinking of
The 'RenderPartial()' method in ASP.NET MVC offeres a very low level of functionality. It
Our win32 application assembles objects from the data in a number of tables in
How often do you find yourself actually using spinlocks in your code? How common

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.