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

  • SEARCH
  • Home
  • 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 97291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:58:36+00:00 2026-05-10T23:58:36+00:00

As we all know, when we derive a class and use polymorphism, someone, somewhere

  • 0

As we all know, when we derive a class and use polymorphism, someone, somewhere needs to know what class to instanciate. We can use factories, a big switch statement, if-else-if, etc. I just learnt from Bill K this is called Dependency Injection.

My Question: Is it good practice to use reflection and attributes as the dependency injection mechanism? That way, the list gets populated dynamically as we add new types.

Here is an example. Please no comment about how loading images can be done other ways, we know.

Suppose we have the following IImageFileFormat interface:

public interface IImageFileFormat {    string[] SupportedFormats { get; };    Image Load(string fileName);    void Save(Image image, string fileName); } 

Different classes will implement this interface:

[FileFormat] public class BmpFileFormat : IImageFileFormat { ... }  [FileFormat] public class JpegFileFormat : IImageFileFormat { ... } 

When a file needs to be loaded or saved, a manager needs to iterate through all known loader and call the Load()/Save() from the appropriate instance depending on their SupportedExtensions.

class ImageLoader {    public Image Load(string fileName)    {       return FindFormat(fileName).Load(fileName);    }     public void Save(Image image, string fileName)    {       FindFormat(fileName).Save(image, fileName);    }     IImageFileFormat FindFormat(string fileName)    {       string extension = Path.GetExtension(fileName);       return formats.First(f => f.SupportedExtensions.Contains(extension));    }     private List<IImageFileFormat> formats; } 

I guess the important point here is whether the list of available loader (formats) should be populated by hand or using reflection.

By hand:

public ImageLoader() {    formats = new List<IImageFileFormat>();    formats.Add(new BmpFileFormat());    formats.Add(new JpegFileFormat()); } 

By reflection:

public ImageLoader() {    formats = new List<IImageFileFormat>();    foreach(Type type in Assembly.GetExecutingAssembly().GetTypes())    {       if(type.GetCustomAttributes(typeof(FileFormatAttribute), false).Length > 0)       {          formats.Add(Activator.CreateInstance(type))       }    } } 

I sometimes use the later and it never occured to me that it could be a very bad idea. Yes, adding new classes is easy, but the mechanic registering those same classes is harder to grasp and therefore maintain than a simple coded-by-hand list.

Please discuss.

  • 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. 2026-05-10T23:58:37+00:00Added an answer on May 10, 2026 at 11:58 pm

    My personal preference is neither – when there is a mapping of classes to some arbitrary string, a configuration file is the place to do it IMHO. This way, you never need to modify the code – especially if you use a dynamic loading mechanism to add new dynamic libraries.

    In general, I always prefer some method that allows me to write code once as much as possible – both your methods require altering already-written/built/deployed code (since your reflection route makes no provision for adding file format loaders in new DLLs).

    Edit by Coincoin:

    Reflection approach could be effectively combined with configuration files to locate the implmentations to be injected.

    • The type could be declared explicitely in the config file using canonical names, similar to MSBuild <UsingTask>
    • The config could locate the assemblies, but then we have to inject all matching types, ala Microsoft Visual Studio Packages.
    • Any other mechanism to match a value or set of condition to the needed type.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Here is a nice article in MSDNMAG and here the… May 11, 2026 at 1:07 pm
  • added an answer You should store as much code as you can in… May 11, 2026 at 1:07 pm
  • added an answer A GoF Façade, much like a real façade, hides the… May 11, 2026 at 1:07 pm

Related Questions

PHP, as we all know is very loosely typed. The language does not require
To summarize, as we all know, a) Silverlight is expected to be hosted by
We all know that having a good note taking tool is important as a
As much as we would all like to say it is a benefit to
I want to know if it is possible to let compiler issue a warning/error
I've been thinking on this for a while now (you know, that dangerous thing
There are lots of mocking frameworks out there for .Net. There is no clear

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.