无法从程序集“CLRviaCSharp,版本=1.0.0.0,文化=中性,PublicKeyToken=null”加载类型“MarshalByRefType”

Posted

技术标签:

【中文标题】无法从程序集“CLRviaCSharp,版本=1.0.0.0,文化=中性,PublicKeyToken=null”加载类型“MarshalByRefType”【英文标题】:Could not load type 'MarshalByRefType' from assembly 'CLRviaCSharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 【发布时间】:2013-02-25 11:40:52 【问题描述】:

这是CLR via C# 3e (Microsoft) 一书中的代码 但它在运行时出错

TypeLoadException 未处理无法加载类型“MarshalByRefType” 从程序集'CLRviaCSharp,版本=1.0.0.0,文化=中性, PublicKeyToken=null'。

除了'Main'方法外,所有代码都是上面提到的书。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Threading;
using System.Reflection;
using System.Runtime.Remoting;

namespace CLRviaCSharp


    class Program
    
        static void Main(string[] args)
        
            Marshalling();
        

        private static void Marshalling()
        
            // Get a reference to the AppDomain that that calling thread is executing in
            AppDomain adCallingThreadDomain = Thread.GetDomain();
            // Every AppDomain is assigned a friendly string name (helpful for debugging)
            // Get this AppDomain’s friendly string name and display it
            String callingDomainName = adCallingThreadDomain.FriendlyName;
            Console.WriteLine("Default AppDomain’s friendly name=0", callingDomainName);
            // Get & display the assembly in our AppDomain that contains the ‘Main’ method
            String exeAssembly = Assembly.GetEntryAssembly().FullName;
            Console.WriteLine("Main assembly=0", exeAssembly);
            // Define a local variable that can refer to an AppDomain
            AppDomain ad2 = null;
            // *** DEMO 1: Cross-AppDomain Communication using Marshal-by-Reference ***
            Console.WriteLine("0Demo #1", Environment.NewLine);
            // Create new AppDomain (security & configuration match current AppDomain)
            ad2 = AppDomain.CreateDomain("AD #2", null, null);
            MarshalByRefType mbrt = null;
            // Load our assembly into the new AppDomain, construct an object, marshal
            // it back to our AD (we really get a reference to a proxy)
            mbrt = (MarshalByRefType)
            ad2.CreateInstanceAndUnwrap(exeAssembly, "MarshalByRefType");
            Console.WriteLine("Type=0", mbrt.GetType()); // The CLR lies about the type
            // Prove that we got a reference to a proxy object
            Console.WriteLine("Is proxy=0", RemotingServices.IsTransparentProxy(mbrt));
            // This looks like we’re calling a method on MarshalByRefType but, we’re not.
            // We’re calling a method on the proxy type. The proxy transitions the thread
            // to the AppDomain owning the object and calls this method on the real object.
            mbrt.SomeMethod();
            // Unload the new AppDomain
            AppDomain.Unload(ad2);
            // mbrt refers to a valid proxy object; the proxy object refers to an invalid AppDomain
            try
            
                // We’re calling a method on the proxy type. The AD is invalid, exception is thrown
                mbrt.SomeMethod();
                Console.WriteLine("Successful call.");
            
            catch (AppDomainUnloadedException)
            
                Console.WriteLine("Failed call.");
            

            // *** DEMO 2: Cross-AppDomain Communication using Marshal-by-Value ***
            Console.WriteLine("0Demo #2", Environment.NewLine);
            // Create new AppDomain (security & configuration match current AppDomain)
            ad2 = AppDomain.CreateDomain("AD #2", null, null);
            // Load our assembly into the new AppDomain, construct an object, marshal
            // it back to our AD (we really get a reference to a proxy)
            mbrt = (MarshalByRefType)
            ad2.CreateInstanceAndUnwrap(exeAssembly, "MarshalByRefType");
            // The object’s method returns a COPY of the returned object;
            // the object is marshaled by value (not be reference).
            MarshalByValType mbvt = mbrt.MethodWithReturn();
            // Prove that we did NOT get a reference to a proxy object
            Console.WriteLine("Is proxy=0", RemotingServices.IsTransparentProxy(mbvt));
            // This looks like we’re calling a method on MarshalByValType and we are.
            Console.WriteLine("Returned object created " + mbvt.ToString());
            // Unload the new AppDomain
            AppDomain.Unload(ad2);
            // mbvt refers to valid object; unloading the AppDomain has no impact.
            try
            
                // We’re calling a method on an object; no exception is thrown
                Console.WriteLine("Returned object created " + mbvt.ToString());
                Console.WriteLine("Successful call.");
            
            catch (AppDomainUnloadedException)
            
                Console.WriteLine("Failed call.");
            

            // DEMO 3: Cross-AppDomain Communication using non-marshalable type ***
            Console.WriteLine("0Demo #3", Environment.NewLine);
            // Create new AppDomain (security & configuration match current AppDomain)
            ad2 = AppDomain.CreateDomain("AD #2", null, null);
            // Load our assembly into the new AppDomain, construct an object, marshal
            // it back to our AD (we really get a reference to a proxy)
            mbrt = (MarshalByRefType)
            ad2.CreateInstanceAndUnwrap(exeAssembly, "MarshalByRefType");
            // The object’s method returns an non-marshalable object; exception
            NonMarshalableType nmt = mbrt.MethodArgAndReturn(callingDomainName);
            // We won’t get here...
        
    

    // Instances can be marshaled-by-reference across AppDomain boundaries
    public sealed class MarshalByRefType : MarshalByRefObject
    
        public MarshalByRefType()
        
            Console.WriteLine("0 ctor running in 1",
            this.GetType().ToString(), Thread.GetDomain().FriendlyName);
        
        public void SomeMethod()
        
            Console.WriteLine("Executing in " + Thread.GetDomain().FriendlyName);
        
        public MarshalByValType MethodWithReturn()
        
            Console.WriteLine("Executing in " + Thread.GetDomain().FriendlyName);
            MarshalByValType t = new MarshalByValType();
            return t;
        
        public NonMarshalableType MethodArgAndReturn(String callingDomainName)
        
            // NOTE: callingDomainName is [Serializable]
            Console.WriteLine("Calling from ‘0’ to ‘1’.",
            callingDomainName, Thread.GetDomain().FriendlyName);
            NonMarshalableType t = new NonMarshalableType();
            return t;
        
    
    // Instances can be marshaled-by-value across AppDomain boundaries
    [Serializable]
    public sealed class MarshalByValType : Object
    
        private DateTime m_creationTime = DateTime.Now; // NOTE: DateTime is [Serializable]
        public MarshalByValType()
        
            Console.WriteLine("0 ctor running in 1, Created on 2:D",
            this.GetType().ToString(),
            Thread.GetDomain().FriendlyName,
            m_creationTime);
        
        public override String ToString()
        
            return m_creationTime.ToLongDateString();
        
    
    // Instances cannot be marshaled across AppDomain boundaries
    // [Serializable]
    public sealed class NonMarshalableType : Object
    
        public NonMarshalableType()
        
            Console.WriteLine("Executing in " + Thread.GetDomain().FriendlyName);
        
    

【问题讨论】:

在 CreateInstanceAndUnwrap 调用中,您需要指定类型的完全限定名称。试试“CLRviaCSharp.MarshalByRefType”。 是的,这就是问题所在。谢谢分配。 【参考方案1】:

当您使用AppDomain.CreateInstanceAndUnwrap 方法时,您应该将类​​型的全名传递给typeName 参数。 所以,替换

ad2.CreateInstanceAndUnwrap(exeAssembly, "MarshalByRefType");

ad2.CreateInstanceAndUnwrap(exeAssembly, typeof(MarshalByRefType).FullName);

【讨论】:

@Idp 谢谢你,你也救了我!

以上是关于无法从程序集“CLRviaCSharp,版本=1.0.0.0,文化=中性,PublicKeyToken=null”加载类型“MarshalByRefType”的主要内容,如果未能解决你的问题,请参考以下文章

C# - 无法从 ResourceManager 获取字符串(来自附属程序集)

无法从程序集 PresentationFramework 加载类型“System.Windows.Controls.Primitives.MultiSelector”

从 .Net Core 运行 Powershell - 无法加载文件或程序集 Microsoft.Management.Infrastructure

System.TypeLoadException:“无法从程序集“System.Web”加载类型“System.Web.HttpContextBase”,

部署 Azure 函数时无法从程序集“System.Runtime”加载类型“System.IO.Path”

从 packages.config 迁移到 PackageReferences 导致无法加载文件或程序集“...”或其依赖项之一。访问被拒绝