【发布时间】:2010-07-14 20:02:49
【问题描述】:
考虑以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RemotingNonVirtualCall
{
class Program
{
static void Main(string[] args)
{
var domain = AppDomain.CreateDomain("Second Domain");
A extA = (A)domain.CreateInstanceAndUnwrap(typeof(A).Assembly.FullName, typeof(A).FullName);
Console.WriteLine(extA.CurrentDomain());
}
}
[Serializable]
sealed class A : MarshalByRefObject
{
public string CurrentDomain()
{
return AppDomain.CurrentDomain.FriendlyName;
}
}
}
方法 A::CurrentDomain 是非虚拟的,A 类是密封的。但是 CLR 拦截方法调用并将其重定向到 另一个 实例。怎么可能?这是某种巫毒魔法吗? CLR 在调用从 MarshalByRefObject 类继承的对象的方法中是否有一些异常?它是如何执行的?
感谢您的提前。
【问题讨论】: