无法从程序集“CLRviaCSharp,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null”中加载类型“MarshalByRefType” [英] Could not load type 'MarshalByRefType' from assembly 'CLRviaCSharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'

查看:1034
本文介绍了无法从程序集“CLRviaCSharp,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null”中加载类型“MarshalByRefType”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码存在于 CLR via C#3e(Microsoft)
但是它在运行期间给出错误


TypeLoadException未处理未能从程序集CLRviaCSharp,Version = 1.0.0.0,Culture = neutral,
PublicKeyToken = null'中加载类型'MarshalByRefType'


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

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

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

命名空间CLRviaCSharp
{

类程序
{
static void Main(string [] args)
{
Marshalling();
}

private static void Marshalling()
{
//获取对调用线程在
中执行的AppDomain的引用AppDomain adCallingThreadDomain = Thread.GetDomain();
//为每个AppDomain分配一个友好的字符串名称(有助于调试)
//获取此AppDomain的友好字符串名称并显示
String callingDomainName = adCallingThreadDomain.FriendlyName;
Console.WriteLine(Default AppDomain的友好名称= {0},callingDomainName);
// Get&在我们的AppDomain中显示包含'Main'方法的程序集
String exeAssembly = Assembly.GetEntryAssembly()。FullName;
Console.WriteLine(Main assembly = {0},exeAssembly);
//定义一个可以引用AppDomain的局部变量
AppDomain ad2 = null;
// *** DEMO 1:使用基于引号的跨应用程序域通信***
Console.WriteLine({0} Demo#1,Environment.NewLine);
//创建新的AppDomain(安全性和配置匹配当前AppDomain)
ad2 = AppDomain.CreateDomain(AD#2,null,null);
MarshalByRefType mbrt = null;
//将我们的程序集加载到新的AppDomain中,构造一个对象,将
//返回给我们的AD(我们真的得到了一个代理的引用)
mbrt =(MarshalByRefType)
ad2.CreateInstanceAndUnwrap(exeAssembly,MarshalByRefType);
Console.WriteLine(Type = {0},mbrt.GetType()); // CLR关于类型
//证明我们获得了对代理对象的引用
Console.WriteLine(Is proxy = {0},RemotingServices.IsTransparentProxy(mbrt));
//看起来像我们在MarshalByRefType上调用一个方法,但是我们不是。
//我们在代理类型上调用一个方法。代理将线程
//转换到拥有该对象的AppDomain,并在真实对象上调用此方法。
mbrt.SomeMethod();
//卸载新AppDomain
AppDomain.Unload(ad2);
// mbrt引用一个有效的代理对象;代理对象引用一个无效的AppDomain
try
{
//我们在代理类型上调用一个方法。 AD无效,抛出异常
mbrt.SomeMethod();
Console.WriteLine(Successful call。);
}
catch(AppDomainUnloadedException)
{
Console.WriteLine(Failed call。
}

// ***示例2:使用按值分类的跨应用程序域通信***
Console.WriteLine({0} Demo#2 ,Environment.NewLine);
//创建新的AppDomain(安全性和配置匹配当前AppDomain)
ad2 = AppDomain.CreateDomain(AD#2,null,null);
//将我们的程序集加载到新的AppDomain中,构造一个对象,将
//返回给我们的AD(我们真的得到一个代理的引用)
mbrt =(MarshalByRefType)
ad2.CreateInstanceAndUnwrap(exeAssembly,MarshalByRefType);
//对象的方法返回一个返回的对象的COPY;
//对象按值编组(不是引用)。
MarshalByValType mbvt = mbrt.MethodWithReturn();
//证明我们没有获得对代理对象的引用
Console.WriteLine(Is proxy = {0},RemotingServices.IsTransparentProxy(mbvt));
//看起来像我们在MarshalByValType上调用一个方法,我们是。
Console.WriteLine(Returned object created+ mbvt.ToString());
//卸载新的AppDomain
AppDomain.Unload(ad2);
// mbvt指有效对象;卸载AppDomain没有任何影响。
try
{
//我们在一个对象上调用一个方法;没有异常被抛出
Console.WriteLine(Returned object created+ mbvt.ToString());
Console.WriteLine(Successful call。);
}
catch(AppDomainUnloadedException)
{
Console.WriteLine(Failed call。
}

// DEMO 3:跨应用程序域使用不可扩展类型的通信***
Console.WriteLine({0} Demo#3,Environment.NewLine );
//创建新的AppDomain(安全性和配置匹配当前AppDomain)
ad2 = AppDomain.CreateDomain(AD#2,null,null);
//将我们的程序集加载到新的AppDomain中,构造一个对象,将
//返回给我们的AD(我们真的得到一个代理的引用)
mbrt =(MarshalByRefType)
ad2.CreateInstanceAndUnwrap(exeAssembly,MarshalByRefType);
//对象的方法返回一个不可缩放的对象; exception
NonMarshalableType nmt = mbrt.MethodArgAndReturn(callingDomainName);
//我们不会在这里...
}
}

//实例可以跨AppDomain边界编组引用
public sealed class MarshalByRefType:MarshalByRefObject:MarshalByRefObject
{
public MarshalByRefType()
{
Console.WriteLine({0} ctor在{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)
{
//注意:callingDomainName是[Serializable]
Console.WriteLine(Calling from'{0}到{1}。,
callingDomainName,Thread.GetDomain()。FriendlyName);
NonMarshalableType t = new NonMarshalableType();
return t;
}
}
//实例可以跨AppDomain边界进行编解码
[Serializable]
public sealed class MarshalByValType:Object
{
private DateTime m_creationTime = DateTime.Now; //注意:DateTime是[Serializable]
public MarshalByValType()
{
Console.WriteLine({0} ctor在{1}中运行,创建于{2:D}
this.GetType()。ToString(),
Thread.GetDomain()。FriendlyName,
m_creationTime);
}
public override String ToString()
{
return m_creationTime.ToLongDateString();
}
}
//实例不能跨AppDomain边界编组
// [Serializable]
public sealed class NonMarshalableType:Object
{
public NonMarshalableType()
{
Console.WriteLine(Executing in+ Thread.GetDomain()。FriendlyName);
}
}
}


解决方案>

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

  ad2.CreateInstanceAndUnwrap(exeAssembly,MarshalByRefType); 

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


This was the code present in book CLR via C# 3e (Microsoft) But it is giving error during runtime

TypeLoadException was unhandled Could not load type 'MarshalByRefType' from assembly 'CLRviaCSharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

Except the method 'Main', all code is of the book mentioned above.

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("{0}Demo #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("{0}Demo #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("{0}Demo #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);
        }
    }
}

解决方案

When you useAppDomain.CreateInstanceAndUnwrap method, you should pass a type's full name to the typeName parameter. So, replace

ad2.CreateInstanceAndUnwrap(exeAssembly, "MarshalByRefType");

with

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

这篇关于无法从程序集“CLRviaCSharp,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null”中加载类型“MarshalByRefType”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆