Search code examples
c#.netreflection.net-remotingactivator

.NET Remoting with Reflection


I need to dynamically load an interface assembly that I use on client-side remoting. Something like this.

static void Main(string[] args)
{
  TcpClientChannel clientChannel = new TcpClientChannel();
  ChannelServices.RegisterChannel(clientChannel, false);

  Assembly interfaceAssembly = Assembly.LoadFile("RemotingInterface.dll");
  Type iTheInterface = 
    interfaceAssembly.GetType("RemotingInterface.ITheService");

  RemotingConfiguration.RegisterWellKnownClientType(iTheInterface,
                                        "tcp://localhost:9090/Remotable.rem");
  object wellKnownObject = Activator.GetObject(iTheInterface, 
                                        "tcp://localhost:9090/Remotable.rem");
}

Only I can't seem to grasp how to call any methods as I can't cast the Activator.GetObject. How can I create a proxy of ITheService without knowing the interface at compile-time?


Solution

  • Got an answer from MSDN forums.

    static void Main(string[] args)
    {
      TcpClientChannel clientChannel = new TcpClientChannel();
      ChannelServices.RegisterChannel(clientChannel, false);
    
      Assembly interfaceAssembly = Assembly.LoadFile("RemotingInterface.dll");
      Type iTheInterface = interfaceAssembly.GetType("RemotingInterface.ITheService");
    
      RemotingConfiguration.RegisterWellKnownClientType(iTheInterface,
                                        "tcp://localhost:9090/Remotable.rem");
      object wellKnownObject = Activator.GetObject(iTheInterface, 
                                        "tcp://localhost:9090/Remotable.rem");
    
      MethodInfo m = iTheInterface.GetMethod("MethodName");
      m.Invoke(wellKnownObject, new object[] { "Argument"});
    }