.NET 如何覆盖 .NET Remoting 中的非虚拟方法? [英] How .NET overrides non-virtual method in .NET Remoting?

查看:35
本文介绍了.NET 如何覆盖 .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.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 类继承的对象的方法中是否有一些例外?它是如何执行的?

Method A::CurrentDomain is non-virtual, class A is sealed. But CLR intercepts method call and redirect it to another instance. How it is possible? Is it some sort of voodoo magic? Does CLR make some exception in method calling for object inherited from MarshalByRefObject class? How is it performed?

感谢提前.

推荐答案

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

It's essentially magic, i.e. the ability to do this is built into the .NET runtime. The good news is that your code can also do this, if it needs to: http://msdn.microsoft.com/en-us/library/system.runtime.remoting.proxies.realproxy.aspx

这篇关于.NET 如何覆盖 .NET Remoting 中的非虚拟方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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