Reputation: 28581
The responses I've got to this question have solved the problem I had in that question, but I'm wondering whether it's possible to extend it a bit. For example, if I were to have third parties contributing commands to this system, would there be a way of extending the first answer to my previous question to allow it to load all the commands from all the DLLs in a folder, and then list them in the list box.
Is that possible? Would it be able to work with a List of ICommand (as the answer to my previous question suggested).
Upvotes: 3
Views: 1974
Reputation: 66763
Yes.
Assembly commandAssembly = Assembly.Load("some/path")
var commands = new List<ICommand>();
foreach (Type type in commandAssembly.GetTypes())
{
if (type.GetInterface(typeof(ICommand).FullName) != null)
{
commands.Add((ICommand)Activator.CreateInstance(type));
}
}
However, you will probably run into some restrictions regarding assembly loading. You cannot just load assemblies from anywhere, otherwise you could reimplement something like COM DLL hell.
Upvotes: 5
Reputation: 10827
You can load each assembly in a directory, iterate through each exposed class in the assembly, and then check for the implementation of your ICommand. From there you can then add them to your internal system by creating a new instance of those objects.
Some places for error checking:
Upvotes: 1