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 に答える