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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:31:41+00:00 2026-06-05T16:31:41+00:00

This is a really long one, so hang tight. The idea is that an

  • 0

This is a really long one, so hang tight.

The idea is that an application, when referenced to Core.dll (see below), will create a new object of type Core, and in doing so, the constructor will scan the C:\Problems folder for any DLLs which contain a type that uses the IPlugin interface.

It works when I reference to Core.dll from a console application. It does not work when I do the equivalent from PowerShell, unless Core.dll is in the GAC.

These are the details:

I have an assembly (Core.dll) which is the result of:

public class Core
{
    List<IPlugin> plugins = new List<IPlugin>();

    public Core(string path)
    {
        LoadPlugins(path);
        foreach (IPlugin plugin in plugins)
            plugin.Work();
    }

    void LoadPlugins(string path)
    {
        foreach (string file in Directory.GetFiles(path))
        {
            FileInfo fileInfo = new FileInfo(file);
            Assembly dllAssembly = Assembly.LoadFile(file);
            foreach (Type type in dllAssembly.GetTypes())
            {
                Type typeInterface = type.GetInterface("Problems.IPlugin");
                if (typeInterface != null)
                    plugins.Add((IPlugin)Activator.CreateInstance(
                                    dllAssembly.GetType(type.ToString())));
            }
        }
    }
}

public interface IPlugin
{
    string Name {get; set;}
    void Work();
}

Then, I have a separate assembly, MyPlugin.dll, that contains the following:

public class MyPlugin : IPlugin
{
    public string Name { get; set; }
    public void Work()
    {
        Console.WriteLine("Hello, World. I'm a plugin!");
    }
}

I tested this all with a third project, TestApp.exe (with Core.dll as a reference):

static void Main(string[] args)
{
    Core core = new Core(@"C:\Problems");
    Console.ReadLine();
}

I put both Core.dll and MyPlugin.dll into C:\Problems, and TestApp.exe works like a charm! So, I write this PowerShell script:

Add-Type -Path "C:\Problems\Core.dll"
$core = New-Object Problems.Core "C:\Problems"

This is my result (imagine a bunch of red):

New-Object : Exception calling ".ctor" with "1" argument(s): "Unable to load one or more o
f the requested types. Retrieve the LoaderExceptions property for more information."
At line:1 char:11
+ New-Object <<<<  Problems.Core "C:\Problems"
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

Drilling down into the Exception, I find this:

PS C:\Problems> $Error[0].Exception.InnerException.LoaderExceptions
Could not load file or assembly 'Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

My conclusion so far is that when I’m running GetTypes() as part of the plugin loader, it’s seeing that MyPlugin inherits from Problems.IPlugin, which it doesn’t know where to find. But, didn’t I already load it in Core.dll?

The only way I can get around this is to sign the Core.dll assembly and add it to the GAC, but that is both annoying and does not fit my purposes.

(All classes are in the Problems namespace, and I omitted a ton of cleanup and filtering logic to illustrate the problem.)

  • 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-06-05T16:31:42+00:00Added an answer on June 5, 2026 at 4:31 pm

    You can solve this by making sure you handle assembly resolve issues by looking in the right place, i.e. change your Core class to be something like this:

    public class Core
    {
        List<IPlugin> plugins = new List<IPlugin>();
        string path;
    
    public Core(string path)
    {
        this.path = path;
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
        LoadPlugins();
        foreach (IPlugin plugin in plugins)
            plugin.Work();
    }
    
    Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        var currentAssemblies = AppDomain.CurrentDomain.GetAssemblies();
    
        foreach (var assembly in currentAssemblies)
        {
            if (assembly.FullName == args.Name)
            {
                Console.WriteLine("resolved assembly " + args.Name + " using exising appdomain");
                return assembly;
            }
        }
    
        var file = args.Name.Split(new char[] { ',' })[0].Replace(@"\\", @"\");
        var dll = Path.Combine(path, file) + ".dll";
        Console.WriteLine("resolved assembly " + args.Name + " from " + path);
        return File.Exists(dll) ? Assembly.LoadFrom(dll) : null;
    }
    
    void LoadPlugins()
    {
        foreach (string file in Directory.GetFiles(this.path, "*.dll"))
        {
            FileInfo fileInfo = new FileInfo(file);
            Assembly dllAssembly = Assembly.LoadFile(file);
            foreach (Type type in dllAssembly.GetTypes())
            {
                Type typeInterface = type.GetInterface("Problems.IPlugin");
                if (typeInterface != null)
                    plugins.Add((IPlugin)Activator.CreateInstance(
                                    dllAssembly.GetType(type.ToString())));
            }
        }
    }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

library(ggplot2) my_title = This is a really long title of a plot that I
This one is a long one, bellow is my primary class that holds the
Let's say I have this blob of code that's made to be one long-running
I've ignored this topic for really long time and I would like to get
I have this case <WrapPanel> <CheckBox>Really long name</CheckBox> <CheckBox>Short</CheckBox> <CheckBox>Longer again</CheckBox> <CheckBox>Foo</CheckBox> <Slider MinWidth=200
I found myself keep writing pretty long one-liner code(influenced by shell pipe), like this:
I've got a page that builds up a really long query depending on $_POST
(Sorry if this is a really long question, it said to be specific) The
When editing really long code blocks (which should definitely be refactored anyway, but that's
This may be a really long stretch but would make life a fair bit

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.