如何发送非序列化对象作为参数传递给代理方法。 MarshalByRefObject的包装? [英] How to send nonserializable objects as arguments to a proxy method. MarshalByRefObject wrapper?

查看:267
本文介绍了如何发送非序列化对象作为参数传递给代理方法。 MarshalByRefObject的包装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我目前正在修改一个插件架构,使其利用的AppDomain。我的插件和许多例子我在网上发现的主要区别是,而不是运行并发送其结果返回到主应用程序的插件,我的主要应用是一个将信息发送到插件。

I'm am currently revising a plugin architecture so that it utilizes AppDomains. The main difference between my plugins and the many examples I have found online is that instead of the plugin running and sending its results back to the main application, my main application is the one sending information to the plugin.

由于它目前为,我在一个单独的应用程序域创建装载机的一个实例。那么它所有的正常的初始化,例如加载插件组装。由于这一点,我然后用装载机的代理来调用从主应用程序到新的AppDomain中发送数据的方法。

As it currently stands, I create an instance of the loader in a separate AppDomain. It then does all your normal initialization such as loading the plugin assembly. As this point, I then use the proxy of the loader to call the methods that send the data from the main application to the new AppDomain.

当我出现我的问题试图呼叫具有一个参数类型不序列化的方法,以及在不脱离MarshalByRefObject的派生

My problem occurs when I am attempting to call a method which has an argument type that is not serializable, and which does not derive from MarshalByRefObject.

由于这些类型是从第三方代码和插件希望接收他们,我似乎无法找到一个办法做到这一点。必须是MarshalByRefObject的"发现了这个问题(如何解决"后?在截肢的语言如C#中的好,但多重继承 ),我在想创造某种包装的。 。我在这里的缺点是,我只是不能想出一个办法,使一个没有修改第三方代码

Since these types are from 3rd party code, and the plugins expect to receive them, I cannot seem to find a way do this. After finding this question ( How to solve "Must be MarshalByRefObject" in a good but multiple-inheritance amputated language like C#? ), I was thinking about creating some sort of wrapper. My drawback here is that I simply cant figure out a way to make one without modifying the 3rd party code.

下面是我的问题的例子:

Here is an example of my issue:

// Cant modify this class
class ThirdPartyClass
{
    // The properties are also 3rd party and nonserializable. These are accessed by the plugins.
    public AnotherClass Property1{ get; set; }
    public AnotherClass Property2{ get; set; }

    public ThirdPartyClass(){}
}

class Loader : MarshalByRefObject
{
    private Plugin plugin;

    public Loader()
    {
        Assembly pluginAssembly = Assembly.LoadFrom("Full/Assembly/Path");
        plugin = pluginAssembly.CreateInstance("Full.Plugin.Name") as Plugin;
    }

    public void DoSomething(ThirdPartyClass o)
    {
        ...
        plugin.DoSomethingElse(o);
    }
}

class PluginManager
{
    void Main(ThirdPartyClass o)
    {
        AppDomain pluginAppDomain = AppDomain.CreateDomain("Plugin AppDomain");
        Loader loader = pluginAppDomain.CreateInstanceFromAndUnwrap("Full/Loader/Path", "Full.Loader.Name") as Loader;

        // This is where I would have a problem.
        loader.DoSomething(o);
    }
}



任何帮助将不胜感激。

Any help would be greatly appreciated.

推荐答案

你需要有在AppDomain与第三方对象交互,它反射回来的另一面?如果不是(并根据您的标题听起来好像你不介意他们可序列化),那么为什么不序列化呢?我的意思是如此的相似的东西:

Do you need to have the appdomain interact with the 3rd party object and it reflect back on the other side? If not (and based on your title it sounds like you wouldn't mind them being serializable) then why not serialize them? By that I mean so something similar to:

[Serializable]
class MyThirdPartyClass : ThirdPartyClass, ISerializable
{
    public MyThirdPartyClass()
    {
    }

    protected MyThirdPartyClass(SerializationInfo info, StreamingContext context)
    {
        Property1 = (AnotherClass)info.GetValue("Property1", typeof(AnotherClass));
        Property2 = (AnotherClass)info.GetValue("Property2", typeof(AnotherClass));
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("Property1", Property1);
        info.AddValue("Property2", Property2);
    }
}



当然也有使用这种方法的注意事项,首先它依赖于你能够派生类,其次它需要你的数据手动序列化(在一定程度上)。你可以自动在内部字段这是现实,你需要担心和重建的另一面,有效地手动序列化的唯一的事情,使用反射。

Of course there are caveats with this approach, firstly it relies on you being able to derive the class and secondly it requires you to manually serialize the data (to a degree). You could automate that using reflection on the internal fields which is realistically the only thing you need to worry about and reconstruct on the other side, effectively manually serializing.

您可能已经能够实现一个替代选择,但现在看来,这是不暴露给开发者更改默认的AppDomain间之一:(

You might have been able to implement a surrogate selector but it seems it isn't exposed to the developer to change the default inter appdomain one :(

这篇关于如何发送非序列化对象作为参数传递给代理方法。 MarshalByRefObject的包装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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