使用 [Serializable] 属性或来自 MarshalByRefObject 的子类化? [英] Use the [Serializable] attribute or subclassing from MarshalByRefObject?

查看:23
本文介绍了使用 [Serializable] 属性或来自 MarshalByRefObject 的子类化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 AppDomains 中使用一个对象.

I'd like to use an object across AppDomains.

为此,我可以使用 [Serializeable] 属性:

For this I can use the [Serializeable] attribute:

[Serializable]
class MyClass
{
    public string GetSomeString() { return "someString" }
}

或者来自 MarshalByRefObject 的子类:

Or subclass from MarshalByRefObject:

class MyClass: MarshalByRefObject
{
    public string GetSomeString() { return "someString" }
}

在这两种情况下,我都可以像这样使用这个类:

In both cases I can use the class like this:

AppDomain appDomain = AppDomain.CreateDomain("AppDomain");
MyClass myObject = (MyClass)appDomain.CreateInstanceAndUnwrap(
                   typeof(MyClass).Assembly.FullName,
                   typeof(MyClass).FullName);
Console.WriteLine(myObject.GetSomeString());

为什么这两种方法看起来效果一样?两种方法有什么区别?我什么时候应该偏爱一种方法而不是另一种方法?

从表面上看,我知道两种机制之间存在差异,但是如果有人从灌木丛中跳出来问我这个问题,我无法给他正确的答案.这些问题是相当开放的问题.我希望有人能比我更好地解释它.

At the surface I know that there are differences between both mechanisms, but if someone jumped out of a bush and asked me the question I couldn't give him a proper answer. The questions are quite open questions. I hoped that someone can explain it better than I could do.

推荐答案

使用 MarshallByRef 将在远程 AppDomain 中执行您的方法.当您将 CreateInstanceAndUnwrap 与 Serializable 对象一起使用时,该对象的副本会复制到本地 AppDomain,因此任何方法调用都将在本地 AppDomain 中执行.

Using MarshallByRef will execute your methods in the remote AppDomain. When you use CreateInstanceAndUnwrap with a Serializable object, a copy of the object is made to the local AppDomain, so any method call will be executed in the local AppDomain.

如果您想要在 AppDomain 之间进行通信,请使用 MarshallByRef 方法.

If what you want is to communicate between AppDomains go with the MarshallByRef approach.

示例:

using System;
using System.Reflection;

[Serializable]
public class SerializableClass
{
    public string WhatIsMyAppDomain()
    {
        return AppDomain.CurrentDomain.FriendlyName;
    }
}

public class MarshallByRefClass : MarshalByRefObject
{
    public string WhatIsMyAppDomain()
    {
        return AppDomain.CurrentDomain.FriendlyName;
    }
}    

class Test
{

    static void Main(string[] args)
    {
        AppDomain ad = AppDomain.CreateDomain("OtherAppDomain");

        MarshallByRefClass marshall = (MarshallByRefClass)ad.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, "MarshallByRefClass");
        SerializableClass serializable = (SerializableClass)ad.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, "SerializableClass");

        Console.WriteLine(marshall.WhatIsMyAppDomain());
        Console.WriteLine(serializable.WhatIsMyAppDomain());

    }
}

当您从 MarshallByRef 对象调用 WhatIsMyAppDomain 时,此代码将显示OtherAppDomain",当您从 Serializable 对象调用时,此代码将显示您的默认 AppDomain 名称.

This code will display "OtherAppDomain" when you call WhatIsMyAppDomain from the MarshallByRef object, and your default AppDomain name when you call from the Serializable object.

这篇关于使用 [Serializable] 属性或来自 MarshalByRefObject 的子类化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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