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

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

【问题讨论】:

    标签: c# reflection assemblies app-config appdomain


    【解决方案1】:

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

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

    基础知识

    1. 将程序集名称放入 app.config 文件中
    2. 使用 ConfigurationManager 读取条目并将名称转换为程序中的字符串
    3. 将程序集的名称传递给 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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多