如何解决"必须是MarshalByRefObject的"如C#中的好,但多重继承截肢的语言? [英] How to solve "Must be MarshalByRefObject" in a good but multiple-inheritance amputated language like C#?

查看:268
本文介绍了如何解决"必须是MarshalByRefObject的"如C#中的好,但多重继承截肢的语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决必须是MarshalByRefObject的像C#中的好,但多重继承截肢的语言?



这个问题很简单,在一些情况下,你只需要从这个类(基础设施的要求)继承。
这并不重要,在这里,哪些案件。
那么,你会怎么做,如果你已经从其他类(域模型的要求)遗传吗?



顺便说一句很好的应用框架,四季如春。净始终确保你没有从这个类继承无论你需要申请到您的类什么样的基础设施。



我想知道我会收到-3选票这里? :)


解决方案

在一般你只想做一个对象MarshalByRef如果你打算使用它在远程处理/ WCF的上下文。这通常是,它不是一个痛苦特殊足够的情况下。



假设你有一个一般的类型,你想从中得到和专门它,然后远程派生类型 - 现在你有一个问题,因为要远程处理的对象必须从MarshalByRefObject继承而来,而你原来的通用型没有。因为假设你正在做的二进制继承,你不能改变它,还是因为它本身从基类派生你不能改变?由于提问指出,因为C#(和.NET一般)不允许MI,你不能从两个继承。



简短的回答是,你'重新排序的螺纹。你要么改变普通型自MarshalByRefObject到inhert(或远远不够了,你可以插入到某处有效链),否则你可以想想摆弄代理对象。



例如,你可以创建描述类型的接口的接口的合同,然后再建一个代理类型自MarshalByRefObject继承也实现由组成和代表团的接口实例你的类型(即包装)。然后,您可以远程这将实例化你的类型,做的工作为预期的代理类型的实例 - 但方法都返回类型必须是[Serializable接口]



<$ P。 $ p> 公共接口IMyType
{
字符串的SayHello();
串BaseTypeMethodIWantToUse();
}

公共类的MyType:MyBaseType,IMyType
{
公共字符串的SayHello()
{
返回你好!
}
}

公共类MyRemoteableType:MarshalByRefObject的,IMyType
{
私人的MyType _instance =新的MyType();

公共字符串的SayHello()
{
返回_instance.SayHello();
}

公共字符串BaseTypeMethodIWantToUse()
{
返回_instance.BaseTypeMethodIWantToUse();
}
}



似乎是一个大量的工作,虽然。最后,如​​果你在这种情况下是我建议重新设计或重新考虑。


How to solve "Must be MarshalByRefObject" in a good but multiple-inheritance amputated language like C#?

The problem is very simple, in several cases you just have to inherit from this class (infrastructure requirements). It does not matter here really, which cases. So, what do you do if you've already inherited from some other class (your domain model requirements)?

Btw good application frameworks, like spring.net always make sure you DON'T have to inherit from this class no matter what kind of infrastructure you need to apply to your class.

I would like to know what am I getting -3 votes here for?? :)

解决方案

In general you only want to make an object MarshalByRef if you're going to use it in a Remoting / WCF context. This is usually a special-enough case that it's not a pain.

Suppose you had a general type, and you wanted to derive from it and specialise it, and then remote the derived type - now you have a problem, because to be remoted an object must inherit from MarshalByRefObject, and your original general type didn't. Suppose you can't change it because you're doing binary inheritance, or because it itself derives from a base class you can't change? As the questioner points out, since C# (and .NET in general) doesn't allow MI, you can't inherit from both.

The short answer is that you're sort-of screwed. You either change the general type to inhert from MarshalByRefObject (or go far enough up the chain that you can insert it somewhere effective), or else you can think about mucking about with proxy objects.

You could for example create an interface contract that describes your type's interface, and then build a proxy type inheriting from MarshalByRefObject that also implements that interface by composition and delegation to an instance of your type (ie a wrapper). You could then remote an instance of that proxy type which would instantiate your type and do the work as expected - but all return types from methods have to be [Serializable].

public interface IMyType
{
    string SayHello();
    string BaseTypeMethodIWantToUse();
}

public class MyType : MyBaseType, IMyType
{
    public string SayHello()
    {
        return "Hello!";
    }
}

public class MyRemoteableType : MarshalByRefObject, IMyType
{
    private MyType _instance = new MyType();

    public string SayHello()
    {
        return _instance.SayHello();
    }

    public string BaseTypeMethodIWantToUse()
    {
        return _instance.BaseTypeMethodIWantToUse();
    }
}

Seems like a lot of work, though. Ultimately if you're in this scenario I'd suggest a redesign or a rethink.

这篇关于如何解决&QUOT;必须是MarshalByRefObject的&QUOT;如C#中的好,但多重继承截肢的语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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