![]() |
Platforms, Frameworks & Libraries »
Libraries »
Code Libraries
Beginner
License: The Code Project Open License (CPOL)
Dynamic Loading Made SimpleBy Derek BartramAn article demonstrating quite how easy dynamic loading it (and why you should use it). |
C# (C# 1.0, C# 2.0, C# 3.0), Windows (Win2K, WinXP, Win2003, Vista), .NET (.NET 1.0, .NET 1.1, .NET 2.0, .NET 3.0, .NET 3.5), Win32, Win64, XAML, WPF, DBA, Dev, Design
|
||||||||
|
Advanced Search |
|
|
|
||||||||||||||||
Dynamic Loading is the process of loading code and using it at run time rather than at compile time. Why use it? Simple, dynamic loading makes the process of modifying a program and adding extra functionality later on easier (especially by third-party vendors). Furthermore it makes larger projects far more managable since each section of code functionality is seperate from each other. Many applications use this technique for implementation of plugins, e.g. Visual Studio and CIRIP (see demo link).
The included source shows how to load classes from dll files (dynamical link libraries) at runtime based upon a particular interface.
public static List<object> LoadAssemblies(String path, String interfaceName, bool recursive)
{
List<object> output = new List<object>();
String[] files = new String[0];
if (recursive)
{
files = Directory.GetFiles(path, "*.dll", SearchOption.AllDirectories);
}
else
{
files = Directory.GetFiles(path, "*.dll", SearchOption.TopDirectoryOnly);
}
foreach (String filename in files)
{
Console.WriteLine("Loading " + filename);
try
{
Assembly assembly = Assembly.LoadFrom(filename);
foreach (Type t in assembly.GetTypes())
{
if (t.IsClass && !t.IsAbstract && t.GetInterface(interfaceName) != null)
{
output.Add(Activator.CreateInstance(t));
}
}
}
catch (Exception err)
{
Console.WriteLine("Loading fail:" + err.ToString());
}
finally
{
Console.WriteLine("Loading complete");
}
}
return output;
}
The LoadAssemblies method loads in a single instance of each class implementing interfaceName in dll files found at the path (or in deeper folders if recursive = true is specified).
To use the code the following steps must be taken;
Did you learn anything interesting/fun/annoying while writing the code? Did you do anything particularly clever or wild or zany? No and no, however plugin support is VERY useful, and once you get the hang of it you'll use it alot.
It's the way forward!!!
Version 1.0.0.0 - The one and only release
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 24 Mar 2008 Editor: |
Copyright 2008 by Derek Bartram Everything else Copyright © CodeProject, 1999-2009 Web15 | Advertise on the Code Project |