0

我只是想弄清楚 AppDomain 并将 DLL 从不同位置加载到新创建的 AppDomain 中,并且遇到了困难。我知道我将使用不同的方法,但我仍在试图弄清楚这一点。这是该示例的背景故事。

我在 c:\DLLTest 中有一个名为 MyReadableDLL.dll 的 dll。以下是内容:

namespace MyReadableDLL
{
        public class ReadThis : System.MarshalByRefObject
        {
        public string RetString()
            {
                    return "You read this from 'Read This!'";
            }
        }
}

我在一个单独的项目中创建了一个 DomainBuilder,内容如下:

    class DomainBuilder
    {
        AppDomainSetup domaininfo = new AppDomainSetup();
        AppDomain appDomain;

        public DomainBuilder()
        {
            domaininfo.ApplicationBase = @"c:\DLLtest";
                    appDomain = AppDomain.CreateDomain("MyTestDomain", null, domaininfo);
        var currentdomain = AppDomain.CurrentDomain.FriendlyName;
                var temporarydomain = appDomain.FriendlyName;
                var appbase = appDomain.SetupInformation.ApplicationBase;
                Assembly assembly = appDomain.Load("MyReadableDLL");
    }
}

我不明白为什么当我获得 appDomain.Load 行时它会失败。根据我对 AddDomainSetup 的了解,AppliationBase 设置了用于检查 EXE/DLL 文件的默认目录,并且该文件夹中只有 MyReadableDLL 文件。我已经看到了许多使用 CreateInstanceFromandUnwrap 的解决方案,我确信我会朝那个方向发展。与此同时,我正在尝试找到一个工作示例 1) AddDomain 创建然后 2) AppDomain.Load 到新创建的域中。而已。我错过了什么?

4

1 回答 1

0

在上述场景中出现明显故障的原因有很多。

  1. AppDomian.Load 需要程序集的全名

    "EventsPublisher, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
    
  2. 确保以管理员身份运行程序以克服安全异常

请找到以下了解 AppDomain 的示例,其中显示了在另一个应用程序域中执行代码的最简单方法。

using System;
using System.Reflection;

public class Worker : MarshalByRefObject
{
    public void PrintDomain() 
    { 
        Console.WriteLine("Object is executing in AppDomain \"{0}\"",
            AppDomain.CurrentDomain.FriendlyName); 
    }
}

class Example
{
    public static void Main()
    {
        // Create an ordinary instance in the current AppDomain
        Worker localWorker = new Worker();
        localWorker.PrintDomain();

        // Create a new application domain, create an instance
        // of Worker in the application domain, and execute code
        // there.
        AppDomain ad = AppDomain.CreateDomain("New domain");
        Worker remoteWorker = (Worker) ad.CreateInstanceAndUnwrap(
            typeof(Worker).Assembly.FullName,
            "Worker");
        remoteWorker.PrintDomain();
    }
}

/* This code produces output similar to the following:

Object is executing in AppDomain "source.exe"
Object is executing in AppDomain "New domain"
 */

查看下面的示例以加载程序集

using System;
using System.Reflection;

class Test {

   static void Main() {
      InstantiateINT32(true);      // OK!
   }

   static void InstantiateINT32(bool ignoreCase) {
      try {
         AppDomain currentDomain = AppDomain.CurrentDomain;
         object instance = currentDomain.CreateInstanceAndUnwrap(
            "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
            "SYSTEM.INT32",
            ignoreCase,
            BindingFlags.Default,
            null,
            null,
            null,
            null,
            null
         );
         Console.WriteLine(instance.GetType());
      } catch (TypeLoadException e) {
         Console.WriteLine(e.Message);
      }
   }
}

我写了一篇博客,介绍了相同的介绍。也可以随时查看我的博客以获取更多详细信息。使用 AppDomain 发送事件

于 2018-06-07T19:02:03.343 回答