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

The Archive Base Latest Questions

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

I have an interface IExample , and a set of classes ClassOne , ClassTwo

  • 0

I have an interface IExample, and a set of classes ClassOne, ClassTwo and ClassThree, all defined in different namespaces. I will possibly remove either of the classes, or add a new one in a new place, at a later stage in development.

Now, I want to find all types that implement IExample at runtime, and instantiate them. (I know on beforehand that no class implementing IExample will ever need any constructor arguments, but I don’t know how to specify that in code, so it’s me – not the compiler – that knows…)

Is this possible? How do I go about to do it?

Update: I’ve now tried several of the approaches suggested, but on all of them, the line Activator.CreateInstance(type), I get a System.MissingMethodException because I “cannot create an instance of an interface.” This is my code:

var tasks = AppDomain.CurrentDomain.GetAssemblies()
    .SelectMany(a => a.GetTypes())
    .Where(t => typeof(IBootstrapperTask).IsAssignableFrom(t))

    // This line is where it fails
    .Select(t => Activator.CreateInstance(t) as IBootstrapperTask)

    .ToArray();
new AutoMapperBootstrapper(tasks).Initialize();

Without the as clause I don’t see any exception, but I’m given an object[], and I need an IBootstrapperTask[] for the constructor on the last line in the excerpt. I’ve tried various ways to cast it, but none seem to work.

  • 1 1 Answer
  • 3 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-20T08:28:22+00:00Added an answer on May 20, 2026 at 8:28 am

    This can be done with Reflection. For example

    var interfaceType = typeof(IExample);
    var all = AppDomain.CurrentDomain.GetAssemblies()
      .SelectMany(x => x.GetTypes())
      .Where(x => interfaceType.IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract)
      .Select(x => Activator.CreateInstance(x));
    

    Note: This will only create instances of IExample from assemblies loaded in the current AppDomain. This can be different than all assemblies loaded in the current process. However for many types of applications this will be equivalent.

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

Sidebar

Related Questions

I have next interface public interface IMyInterface { string this[string key] { get; set;
I have interface IModule and several classes that implements it. In test i need
I have a Device interface: public interface IDevice { double Measure(); } Some classes
i have interface: public interface Inx<T> { T Set(Data data); } simple class with
If I have interface and implementing classes like public interface IA {} public class
If I have interface IFoo, and have several classes that implement it, what is
I am planning to develop a site which will have interface very similar to
I have something like: public interface IExample { int GetInteger() T GetAnything(); //How do
Let's say I have interface: public interface IFoo { int Bar1 { get; set;
I have interface IConfigurationSource { .... } and class FileConfigurationSource : IConfigurationSource { FileConfigurationSource(string

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.