Dynamically loading classes (with custom behavior) from different assemblies?

拥有回忆 提交于 2019-12-03 08:24:12

This is how I load plug-ins (add-ins) into one of my projects:

const string PluginTypeName = "MyCompany.MyProject.Contracts.IMyPlugin";

/// <summary>Loads all plugins from a DLL file.</summary>
/// <param name="fileName">The filename of a DLL, e.g. "C:\Prog\MyApp\MyPlugIn.dll"</param>
/// <returns>A list of plugin objects.</returns>
/// <remarks>One DLL can contain several types which implement `IMyPlugin`.</remarks>
public List<IMyPlugin> LoadPluginsFromFile(string fileName)
{
    Assembly asm;
    IMyPlugin plugin;
    List<IMyPlugin> plugins;
    Type tInterface;

    plugins = new List<IMyPlugin>();
    asm = Assembly.LoadFrom(fileName);
    foreach (Type t in asm.GetExportedTypes()) {
        tInterface = t.GetInterface(PluginTypeName);
        if (tInterface != null && (t.Attributes & TypeAttributes.Abstract) !=
            TypeAttributes.Abstract) {

            plugin = (IMyPlugin)Activator.CreateInstance(t);
            plugins.Add(plugin);
        }
    }
    return plugins;
}

I assume that each plug-in implements IMyPlugin. You can define this interface any way you want. If you loop through all DLLs contained in a plug-ins folder and call this method, you can automatically load all the available plug-ins.

Usually you would have at least three assemblies: One containing the interface definition, the main assembly referencing this interface assembly and at least one assembly implementing (and of course referencing) this interface.

Does each customer get their own exe and config file, and there is a shared dll? Or is there a shared exe, and each customer has their own dll?

You can put the full type name in the configuration like this:

Shared.exe.config:

<appSettings>
  <add key="CustomerType" value="NamespaceForJohn.ClassForJohn, AssemblyForJohn"/>
</appSettings>

And put AssemblyForJohn.dll in the same folder as your Shared.exe.

Then you can load it dynamically in code like this:

Shared.exe:

var typeString = ConfigurationManager.AppSettings["CustomerType"];
var parts = typeString.Split(',');
var typeName = parts[0];
var assemblyName = parts[1];
var instance = (BaseClass)Activator.CreateInstance(assemblyName, typeName).Unwrap();

maybe this example will be helpful

public MyInterface GetNewType() { 
       Type type = Type.GetType( "MyClass", true ); 
       object newInstance = Activator.CreateInstance( type ); 
       return newInstance as MyInterface; 
    } 

Here's one way to handle it with DI using Unity.

IUnityContainer container = new UnityContainer();
string customerNamespace = ConfigurationManager.AppSettings["CustomerNamespace"];
container.RegisterType(typeof(ISomeInterface), 
                       Type.GetType(customerNamespace+".SomeImplementation"));


// ...

ISomeInterface instance = conainer.Resolve<ISomeInterface>();

Where each customer has their own implementation of ISomeInterface in a customer specific namespace.

You can create an instance of an external type from an assembly this way:

object obj = Activator.CreateInstance( 
    "External.Assembly.Name", "External.Assembly.Name.TypeName");
BaseClass b = (BaseClass) obj;
b.getEventId();

You'd store the name of the assembly and type in your configuration file or some other appropriate place.

granadaCoder

I would use Unity, but as a Simple Factory.

Unity Framework: How to Instantiate two classes from the same Interface?

You could store your

I am using Unity.2.1.505.2 (just in case that makes a difference).

  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>


      <unity>
        <container>
          <register type="IVehicle" mapTo="Car" name="myCarKey" />
          <register type="IVehicle" mapTo="Truck" name="myTruckKey" />
        </container>
      </unity>

Here is the DotNet code.

UnityContainer container = new UnityContainer();

UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
                section.Configure(container);

string myKey = "John";  /* read from config file */ /* with this example, the value should be "myCarKey" or "myTruckKey"  */

IVehicle v1 = container.Resolve<IVehicle>(myKey); 

See:

http://msdn.microsoft.com/en-us/library/ff664762(v=pandp.50).aspx

and

http://www.sharpfellows.com/post/Unity-IoC-Container-.aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!