RemotingServices.Unmarshal and Interfaces

B

Bill

When passing ObjRef's between AppDomains I have seen a
possible problem with Unmarshalling: When marshalling a
class an an interface, it must be marshalled at one least
class more derived than it will be cast as after
unmarshalling, unless GetType() is called before casting.

---- Code Example --- (tested on Framework 1.1)

public interface IA {...}

public interface IB : IA {...} // note: IB may be empty

public class C : MarshalByRefObj, IB {...}

// on server side

C c = new C();

ObjRef or = RemotingServices.Marshal(c, "MyURI", typeof
(IB) );

// on client side

object objC = RemotingServices.Unmarshal(or);

IA ia = (IA)objC; // works fine

IB ib = (IB)objC; // throws an invalid cast exception

// unless:

objC.GetType();
IB ib = (IB)objC; // now will work.


------

This derived class casting will work Ok if they are base
classes and not interfaces:

class A : MarshalByRefObject {}

class B : A {}

class C: B {}

C c = new C();

RemotingServices.MarshalByRef( c, "MyURI", typeof(B) );

will unmarshall ok and cast to A or B w/o the GetType()
trick.

Is the problem that the type on Marshal(o,uri,type) must
always be derived from MBO? If so too bad, it would be
nice to abstract as interfaces.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top