C# .NET如何在.NET远程处理中重写非虚拟方法?
考虑以下代码:C# .NET如何在.NET远程处理中重写非虚拟方法?,c#,.net,remoting,C#,.net,Remoting,考虑以下代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace RemotingNonVirtualCall { class Program { static void Main(string[] args) { var domain = AppDomain.CreateDoma
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类继承的对象的方法中是否出现异常?它是如何执行的
谢谢你提前通知 它本质上是神奇的,也就是说,这种能力内置于.NET运行时中。好消息是,如果需要,您的代码也可以做到这一点:这本质上是一种魔力,也就是说,实现这一点的能力内置于.NET运行时中。好消息是,如果需要,您的代码也可以做到这一点:JIT编译器非常清楚它为代理生成代码。您可以查看SSCLI20源代码clr\src\vm\jithelpers.cpp,搜索“proxy”。JIT编译器清楚地知道它为代理生成代码。您可以查看SSCLI20源代码,clr\src\vm\jithelpers.cpp,搜索“代理”