xNidhogg
xNidhogg

Reputation: 341

What is the 2013 way to load an assembly at runtime?

I want to load several assemblies at runtime from my plugin directory. The core app does not have references to the assemblies beforehand, the assemblies implement an interface given by the core app, thus have a reference to it. I found and article from 2005 explaining exactly what I need to do to make this work.

Currently I have this code, which is basically a LINQ'd version of what you find in the article above:

foreach (
    IModule module in
        Directory.EnumerateDirectories(string.Format(@"{0}/{1}", Application.StartupPath, ModulePath))
            .Select(dir => Directory.GetFiles(dir, "*.dll"))
            .SelectMany(files => files.Select(Assembly.LoadFrom)
                                        .SelectMany(assembly => (from type in assembly.GetTypes()
                                                                where type.IsClass && !type.IsNotPublic
                                                                let interfaces = type.GetInterfaces()
                                                                where
                                                                    ((IList) interfaces).Contains(
                                                                        typeof (IModule))
                                                                select
                                                                    (IModule) Activator.CreateInstance(type))))
    )
{
    module.Core = this;
    module.Initialize();
    AddModule(module);
}

So what is the current way to dynamically load plugins/modules/assemblies at runtime?

Upvotes: 0

Views: 159

Answers (3)

Marko
Marko

Reputation: 11002

Doesn't the roslyn project provide with some helpful things?

http://msdn.microsoft.com/en-us/vstudio/roslyn.aspx

http://www.developerfusion.com/article/143896/understanding-the-basics-of-roslyn/

var scriptEngine = new ScriptEngine();
var session = Session.Create();
scriptEngine.Execute("foo.dll", session);

Upvotes: 0

Luke Marlin
Luke Marlin

Reputation: 1398

I'd suggest Managed Extensibility Framework

Alternatively, if you need to load "plugins" with no need to keep a reference, there is also PRISM

Upvotes: 0

dalexsoto
dalexsoto

Reputation: 3412

You might want to look at Microsoft Managed Extensibility Framework also this Tutorial its kinda nice intro.

Upvotes: 3

Related Questions