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

时间:2012-04-22 22:57:48

标签: c# reflection assemblies app-config appdomain

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

1 个答案:

答案 0 :(得分:2)

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

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

基本

  1. 将程序集的名称放在app.config文件中
  2. 使用ConfigurationManager读取条目并将名称输入程序中的字符串
  3. 将程序集的名称传递给Assembly.Load方法。
  4. 链接示例:

    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);
        }
    }
    
相关问题