从不同程序集中动态加载类(具有自定义行为)吗? [英] Dynamically loading classes (with custom behavior) from different assemblies?

查看:83
本文介绍了从不同程序集中动态加载类(具有自定义行为)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在为少数客户构建应用程序,每个客户都有自己的要求以及类似的要求.我们还希望将所有代码保留在同一个应用程序中,而不是对其进行分支,并且IF并不是很好的选择,因为它会在各处出现.

We are building an app for few customers, each has its own requirements along with the similar ones. We also want to keep all the code in the same app, not branch it, and the IFs is not good choice since it will be all over places.

我计划为所有人提供基类.然后,每个客户将拥有自己的类,其中的重写方法将执行特殊的逻辑.

I plan having the base classes for all. Then each customer will have its own class in which the override methods will do special logic.

在编译时如何加载程序集而不是这样做

How can we load the assemblies when compiling instead of doing this

public class BaseClass {
    public string getEventId()
}

public class ClassForJohn:BaseClass {
    [override]
    public string getEventId()
}

public class ClassForAdam:BaseClass {
    [override]
    public string getEventId()
}

void UglyBranchingLogicSomewhere() {
   BaseClass  eventOject;
   if("John"==ConfigurationManager.AppSettings["CustomerName"]){
        eventOject = new ClassForJohn();


   }else if("Adam"==ConfigurationManager.AppSettings["CustomerName"]){
        eventOject = new ClassForAdam();


   }else{
        eventOject = new BaseClass ();

   }
  eventId = eventOject.getEventId();
}

推荐答案

这是我将插件(加载项)加载到我的一个项目中的方式:

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;
}

我假设每个插件都实现了IMyPlugin.您可以根据需要定义此接口.如果遍历插件文件夹中包含的所有DLL并调用此方法,则可以自动加载所有可用的插件.

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.

这篇关于从不同程序集中动态加载类(具有自定义行为)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆