3

我需要动态加载我在客户端远程处理中使用的接口程序集。像这样的东西。

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");
}

只是我似乎无法掌握如何调用任何方法,因为我无法转换 Activator.GetObject。如何在编译时不知道接口的情况下创建 ITheService 的代理?

4

4 回答 4

3

从MSDN 论坛得到答案。

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"});
}
于 2011-05-23T09:05:41.900 回答
0

返回的对象实现了接口,因此您可以使用反射来获取其成员方法并调用它们。

或者,在 C#4 中,您可以使用dynamic

dynamic wellKnownObject = Activator.GetObject(iTheInterface, 
    "tcp://localhost:9090/Remotable.rem");

wellKnownObject.SomeMethod(etc ..);
于 2011-05-16T21:00:49.983 回答
0

首先,检查对象的可用方法/接口:

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

var objType = wellKnownObject.GetType();
var methods = objType.GetMethods();
var interfaces = objType.GetInterfaces();

在确定要调用的方法之后,

  1. 考虑使用DLR和/或将动态对象包装在 DynamicObject 容器中。
  2. methods[i].Invoke在物体上使用。

这里有些例子:

namespace ConsoleApplication1
{
  using System;

  class Program
  {

    static void Main()
    {
      //Using reflection:
      object obj = GetUnknownObject();
      var objType = obj.GetType();

      var knownInterface = objType.GetInterface("IA");
      var method = knownInterface.GetMethod("Print");
      method.Invoke(obj, new object[] { "Using reflection" });

      //Using DRL
      dynamic dObj = GetUnknownObject();
      dObj.Print("Using DLR");

      //Using a wrapper, so you the dirty dynamic code stays outside
      Marshal marshal = new Marshal(GetUnknownObject());
      marshal.Print("Using a wrapper");

    }

    static object GetUnknownObject()
    {
      return new A();
    }
  } //class Program

  class Marshal
  {
    readonly dynamic unknownObject;
    public Marshal(object unknownObject)
    {
      this.unknownObject = unknownObject;
    }

    public void Print(string text)
    {
      unknownObject.Print(text);
    }
  }

  #region Unknown Types
  interface IA
  {
    void Print(string text);
  }

  class A : IA
  {
    public void Print(string text)
    {
      Console.WriteLine(text);
      Console.ReadKey();
    }
  }
  #endregion Unknown Types
}
于 2011-05-16T21:01:23.393 回答
0

我可以从http://localhost:8080/xxx.rem?wsdl等远程 URL 获取接口信息吗?

作为WebService,我可以从服务url http://xXX.xx.xxx.xx/url.svc?wsdl中获取接口信息,自己编译代码,通过反射调用方法。

于 2012-03-26T06:52:51.577 回答