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 6762733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:23:33+00:00 2026-05-26T14:23:33+00:00

Given a .net application that supports different logic providers, how to instantiate one instance

  • 0

Given a .net application that supports different logic providers, how to instantiate one instance of each class in a solution folder? What is the way to iterate through all of the classes in the folder?

For example I have a folder in my solution called MailClientProviders
It contains Outlook and The Bat! provider classes that implement a IMailProvider interface.

In my App.xaml I call a Ninject container to initialize all the dependencies. Then I need to write a method that I would call, and would get an instance of each class returned.

heartbeatService.Providers = CreateOneInstanceOfAllClassesInProvidresDir(MailClientProviders);

What would be in the CreateOneInstanceOfAllClassesInProvidresDir method?

  • 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-26T14:23:34+00:00Added an answer on May 26, 2026 at 2:23 pm

    I used these functions to retrieve all classes in a folder implementing my custom interface:

    public static List<T> GetFilePlugins<T>(string filename)
    {
        List<T> ret = new List<T>();
        if (File.Exists(filename))
        {
            Type typeT = typeof(T);
            Assembly ass = Assembly.LoadFrom(filename);
            foreach (Type type in ass.GetTypes())
            {
                if (!type.IsClass || type.IsNotPublic) continue;
                if (typeT.IsAssignableFrom(type))
                {
                    T plugin = (T)Activator.CreateInstance(type);
                    ret.Add(plugin);
                }
            }
        }
        return ret;
    }
    public static List<T> GetDirectoryPlugins<T>(string dirname)
    {
        List<T> ret = new List<T>();
        string[] dlls = Directory.GetFiles(dirname, "*.dll");
        foreach (string dll in dlls)
        {
            List<T> dll_plugins = GetFilePlugins<T>(Path.GetFullPath(dll));
            ret.AddRange(dll_plugins);
        }
        return ret;
    }
    

    So you can run GetDirectoryPlugins<IMailProvider> and use Activator.CreateInstance with every class found…

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

Sidebar

Related Questions

I am building a .NET application that given a connection string, at run time,
I have a C# (2008/.NET 3.5) class library assembly that supports WPF (based on
Given an ASP.NET MVC view that generates a table of entries using a for
I was given a C++ project that was compiled using MS Visual Studio .net
Given: ASP.Net application which uses a legacy COM library legacy COM library which is
I am looking for different techniques/tools you use to deploy an ASP.NET web application
We have developed a .NET web application that uses SQL Server as a backend.
I am writing an MFC application that doesn't use .NET (CLR support is set
I am writing a C# .NET server application that sends and receives data over
In a C#/ASP.NET application, I have a form that renders much like the following

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.