从 App.Config 将程序集加载到 AppDomain

Posted

技术标签:

【中文标题】从 App.Config 将程序集加载到 AppDomain【英文标题】:Load Assembly into AppDomain from App.Config 【发布时间】:2012-04-22 22:57:47 【问题描述】:

我有一个 DLL,我需要成为当前 AppDomain 的一部分。有没有办法让 AppDomain 从 app.config/web.config 中的某个列表中获取 dll?

【问题讨论】:

【参考方案1】:

您可以将程序集的名称放在 app.config 文件中,然后在运行时使用 Assembly.Load 选项和反射加载它。这是 MSDN 文章的链接,该文章描述了如何执行此操作。

http://msdn.microsoft.com/en-us/library/25y1ya39.aspx

基础知识

    将程序集名称放入 app.config 文件中 使用 ConfigurationManager 读取条目并将名称转换为程序中的字符串 将程序集的名称传递给 Assembly.Load 方法。

链接示例:

public class Asmload0

    public static void Main()
    
        // Use the file name to load the assembly into the current
        // application domain.
        Assembly a = Assembly.Load("example");
        // Get the type to use.
        Type myType = a.GetType("Example");
        // Get the method to call.
        MethodInfo myMethod = myType.GetMethod("MethodA");
        // Create an instance.
        object obj = Activator.CreateInstance(myType);
        // Execute the method.
        myMethod.Invoke(obj, null);
    

【讨论】:

以上是关于从 App.Config 将程序集加载到 AppDomain的主要内容,如果未能解决你的问题,请参考以下文章