简体   繁体   English

无法从程序集中加载类型

[英]Cannot load type from assembly

I'm studying this code from MSDN MarshalByRefObject I couldn't figure out why this is throwing an exception. 我正在研究来自MSDN MarshalByRefObject的代码,我不知道为什么会引发异常。 I'm thinking if it could be the Worker type is in the same assembly as the class Program? 我在想,是否可能是Worker类型与Program类位于同一程序集中? Do I need to install this assembly in the GAC, though I'm running this in debug mode in Visual Studio 2017 editor. 我是否需要在GAC中安装此程序集,尽管我正在Visual Studio 2017编辑器中以调试模式运行此程序集。 I don't have a solid understanding of how to work with assembly. 我对如何使用汇编没有深刻的了解。

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);
        }
    }
}

You instantiate an incomplete typename . 您实例化了一个不完整的typename Add this to your code: 将此添加到您的代码:

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);

        ...
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM