2

Is it possible to determine the interfaces that can be used to cast a MarshalByRefObject object?

How does the cast operator work with MarshalByRefObject objects? Does it call the CreateObjRef method?

Thanks, Massimo

Massimo Zerbini
  • 3,125
  • 22
  • 22

2 Answers2

2

Here's a workaround that can be used to retrieve the list of interfaces.

Define a public interface IDescriptor

public interface IDescriptor
{
   List<string> GetInterfaces();
}

Define a base class that implements the interface:

public class BaseMasrhalByRefObject : MasrhalByRefObject, IDescriptor
{
   public BaseMasrhalByRefObject() : base() {}

   public List<string> GetInterfaces()
   {
      List<string> types = new List<string>();
      foreach(Type i in GetType().GetInterfaces())
      {
         types.Add(i.AssemblyQualifiedName);
      }
      return types;
   }
}

Than use the BaseMasrhalByRefObject instead of MasrhalByRefObject to define a service object:

public class MyServiceObject : BaseMasrhalByRefObject, MyInterface1, MyInterface2, ...
{
      // Add logic method
}

In AppDomain A create the reference object of MyServiceObject. In AppDomain B get the proxy of the remote object. The proxy can be cast to IDescriptor:

public List<Type> GetInterfaces(MasrhalByRefObject proxy)
{
   List<Type> types = new List<Type>();
   IDescriptor des = proxy as IDescriptor;
   if (des != null)
   {
      foreach(string t in des.GetInterfaces()) // this is a remote call
      {
         types.Add(Type.GetType(t);
      }
   }
   return types;
}
Massimo Zerbini
  • 3,125
  • 22
  • 22
0

MarshalByRefObject is a class so having only an interface you can't be sure if all classes implementing it derive from MarshalByRefObject. However, if you have an instance of an object you can easily check that using this expression:

if (obj1 is MarshalByRefObject)
{
    // do your thing
}
Bartosz Wójtowicz
  • 1,321
  • 10
  • 18
  • I mean how I can find all interfaces that can be use to cast an object that is an instance of MasrhalByRefObject. I know that the object is an instance of MasrhalByRefObject, I don't need to test this condition. The problem is that the method GetType().GetInterfaces() return a void array, but I can cast the obj to an interface MyInterface. – Massimo Zerbini Dec 17 '11 at 21:08
  • Ok, I understand your problem now. How are you creating that object (is it using Activator to get a proxy to the remote object?) and what type does the GetType() method return in your case? – Bartosz Wójtowicz Dec 19 '11 at 09:50
  • If the object you've got is a proxy to a remote object created using `Activator.GetObject(...)` then you should really know the interface it's exposing as it is registered as the wellKnownType. Alternatively, perhaps you could try getting the RealProxy and calling some protected methods to get the interface. This post might help you: [http://stackoverflow.com/a/7060298/1014643](http://stackoverflow.com/a/7060298/1014643) – Bartosz Wójtowicz Dec 19 '11 at 11:02
  • Thanks for this tips Bartosz. I can not call the RealProxy protected method but I've found a workaround to solve my problem. – Massimo Zerbini Dec 19 '11 at 15:25