【问题标题】:How .NET overrides non-virtual method in .NET Remoting?.NET 如何覆盖 .NET Remoting 中的非虚拟方法?
【发布时间】: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 类继承的对象的方法中是否有一些异常?它是如何执行的?

感谢您的提前。

【问题讨论】:

    标签: c# .net remoting


    【解决方案1】:

    这本质上是一种魔法,也就是说,执行此操作的能力内置于 .NET 运行时中。好消息是,如果需要,您的代码也可以这样做:http://msdn.microsoft.com/en-us/library/system.runtime.remoting.proxies.realproxy.aspx

    【讨论】:

      【解决方案2】:

      JIT 编译器敏锐地意识到它会为代理生成代码。你可以看看SSCLI20源码,clr\src\vm\jithelpers.cpp,搜索“proxy”。

      【讨论】:

        猜你喜欢
        • 2012-06-19
        • 2018-01-28
        • 2012-12-08
        • 2011-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多