无法从程序集加载类型

Cannot load type from assembly

我正在研究来自 MSDN MarshalByRefObject 的这段代码,我无法弄清楚为什么会抛出异常。我在想是否可能是 Worker 类型与 class 程序在同一个程序集中?我是否需要在 GAC 中安装此程序集,尽管我 运行 在 Visual Studio 2017 编辑器中处于调试模式。我对如何使用汇编没有很好的理解。

using System;
using System.Reflection;

namespace AppDomainMarshalByRefObject
{

    public class Program
    {
        public static void Main(string[] args)
        {
            Worker localWorker = new Worker();
            localWorker.PrintDomain();

            AppDomain ad = AppDomain.CreateDomain("New domain");
            Worker remoteWokrer = (Worker)ad.CreateInstanceAndUnwrap(typeof(Worker).Assembly.FullName, "Worker");

            remoteWokrer.PrintDomain();
            Console.ReadKey();
        }
    }

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

您实例化了一个不完整的 typename。将此添加到您的代码中:

public static void Main(string[] args)
    {
        ...

        var typeName = typeof(Worker);

        AppDomain ad = AppDomain.CreateDomain("New domain");
        Worker remoteWokrer = (Worker)ad.CreateInstanceAndUnwrap(typeof(Worker).Assembly.FullName, typeName.FullName);

        ...
    }