cyrus-d
cyrus-d

Reputation: 843

Load assembly into application

I am trying to implement a plugin application into my MVC application, I have managed to sort out the view engines and inject a new controller from the assembly into application with the use of MEf.

However the assembly I am trying to load also has a strongly type module that I would like to inject into the plugin application I am loading the assembly as follow:

enter image description here

I can tell the assembly is loaded into current appdomain but still I am getting following error:

Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0246: The type or namespace name 'cars' could not be found (are you missing a using directive or an assembly reference?)

I would greatly appreciate it if you kindly give me some hint.

Thank you very much.

Upvotes: 0

Views: 306

Answers (1)

Pharap
Pharap

Reputation: 4082

Your issue lies in the inclusion of a non-referenced library, though in your case as you are trying to dynamically load a library the solution is to remove the the using directive.

To properly manage plugins you should expose a common API in the form of a library. The API should provide interfaces and classes that are abstract enough that they can be used by any plugin to properly register itself. They should expose enough to perform the actions you would expect or allow (i.e. changing the interface, adding a way to handle certain types of content), but should not expose large amounts of inner workings or secure information.

There are two ways you can define a plugin, you can have them as .exes or .dlls.

If you define them as .exes you can make them accept classes defined in the plugin API that they then use to register themselves. Example:

Main Program:

void LoadPlugin(string fullPath)
{
Assembly assembly = Assembly.FromFile(fullPath);
//class that provides methods for registering plugins
IPluginRegistrationService registration = new PluginRegistrationService();
assembly.EntryPoint.Invoke(null,new object[] { registration });
}

Plugin:

static void main(IPluginRegistrationService registration)
{
//do registration work
//for example:
//registration.RegisterUIPlugin(new UIPluginInfo("plugin name"));
}

If you define them as .dlls, the program itself must search for and instantiate classes that implement an interface/class that defines the plugin's entry. Example:

MainProgram:

void LoadPlugin(string fullPath)
{
Assembly assembly = Assembly.LoadFile(file);
foreach (Module module in assembly.GetModules())
{
foreach (Type type in module.GetTypes())
{
if (type.IsSubclassOf(typeof(IPlugin)))
{ 
//poll constructors and instantiate type
//do work to load plugin based on values
}
}
}
}

Plugin:

public class CarPlugin : IPlugin
{
//implement IPlugin members to expose
//information such as name 
}

Upvotes: 2

Related Questions