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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:40:05+00:00 2026-05-31T07:40:05+00:00

I got all the classes that implement my interface. I want to call a

  • 0

I got all the classes that implement my interface. I want to call a method when the instance of the object is created

var types =AppDomain.CurrentDomain.GetAssemblies().ToList()
            .SelectMany(s => s.GetTypes())
            .Where(t => typeof(IManagerReport).IsAssignableFrom(t));

Console.WriteLine("Processing manager reports..");
foreach(var TheType in types)
{
              //error here
    var temptype = Activator.CreateInstance(TheType) as IManagerReport;

    temptype.Load();
    temptype.Save();
    Console.WriteLine("Saved to: " + temptype.SavePath);
}

The error that is produced is here:

Cannot create an instance of an interface

  • 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-31T07:40:06+00:00Added an answer on May 31, 2026 at 7:40 am

    Make sure you exclude IManagerReport from the selection of types you’re fetching.

    var types =AppDomain.CurrentDomain.GetAssemblies().ToList()
                .SelectMany(s => s.GetTypes())
                .Where(t => typeof(IManagerReport).IsAssignableFrom(t)
                            && typeof(IManagerReport) != t
                            && !t.IsInterface
                            && !t.IsAbstract
                            && !t.IsGenericTypeDefinition);
    

    The problem is, in your types enumerable, you’ve got not only the derived types of IManagerReport, but also the IManagerReport itself. You can’t create an instance of an interface, only a class. Use the code I posted to exclude most trouble items, but I would still add a try/catch on the Activator.CreateInstance. You may have derived types that have no parameterless public constructor. These would also fail.

    Wrap the call in a try/catch and continue. I would say you should account for all possibilities here, but there’s just simply too many of them. Account for a few, then also account for the possibility that the creation simply won’t work for another reason.

    try {
        var temptype = Activator.CreateInstance(TheType) as IManagerReport;
    } catch {
        continue;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i've got 3 Log classes that all implements iLog interface: DatabaseLog FileLog ScreenLog there
I want to resolve in Windsor a series of classes that implement this interface:
I've got a group of inter-related classes that are all overridden together to create
I have to implement classes that work with payment system (let's call it PaymentSystem)
Got a class that serializes into xml with XMLEncoder nicely with all the variables
I've got a bunch of classes that do different operations over the network, and
I've got some code that uses the Component Categories manager to load all of
I am writing a game in ActionScript where I've got multiple classes that should
I've got an interface interface IModule { public function Install(); } and some classes
I've got all the tutorials up to beginner #5 translated and working, but I

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.