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

前端 未结 5 578
我在风中等你
我在风中等你 2020-12-18 19:55

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

相关标签:
5条回答
  • 2020-12-18 20:00

    I had success with a generic approach. T doesn't have to be "MarshalByRefObject". Of course you'll have to replace "RemoteProcess" with the object you use for remoting. Then you can access your non-MarshalByRefObject as RemotingHost.RemoteObject.

    public class RemotingHost<T> : MarshalByRefObject where T: class
    {
        RemoteProcess host;
        T remoteObject;
        public T RemoteObject { get { return remoteObject; } }
    
        public RemotingAdmin()
        {
            host = new RemoteProcess();
            remoteObject = (T)host.CreateObject(typeof(T));
        }
    }
    
    0 讨论(0)
  • 2020-12-18 20:05

    "So, what do you do if you've already inherited from some other class (your domain model requirements)?"

    Can you create a base class for the piece of your domain model that requires inheritance from MarshalByRefObject?

    0 讨论(0)
  • 2020-12-18 20:19

    It depends on how you need to get at it. Using a base class that derives from MarshalByRefObject might do it. Aggregation might do it. Without a more concrete example of what you need it's hard to say, but it's a rare case that multiple inheritance would be the only solution to a problem.

    0 讨论(0)
  • 2020-12-18 20:21

    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.

    0 讨论(0)
  • 2020-12-18 20:27

    You can't inherit from multiple classes. So you either need to (a) change your inheritance hierarchy so the base inherits from it, or (b) write your application differently.

    Without more information about why you need to inherit from MarshalByRefObject or why your base class does not (cannot?) then it's hard to give any more concrete advice.

    But I'd say if you have a derived type that needs different marshaling semantics to its base, then you've probably got an architectural issue somewhere.

    0 讨论(0)
提交回复
热议问题