技术开发 频道

ICE框架之Slilce2CSharp映射---操作的映射


【IT168技术文档】


  对于接口上的每个操作,代理类都有一个对应的同名成员函数。要调用某个操作,你要通过代理调用它。例如,下面给出的文件系统的部分定义 :
module Filesystem { interface Node { nonmutating string name(); }; // ... };
  name操作返回的是类型为 string 的值。假定我们有一个代理,指向的是Node 类型的对象,客户可以这样调用操作:
NodePrx node = ...; // Initialize proxy string name = node.name(); // Get name via RPC
  这是典型的返回值接收模式:对于复杂的类型,返回值通过引用返回,对于简单的类型 (比如 int或 double)则直接返回值。。

  普通的、idempotent,以及nonmutating操作

  你可以给 Slice 操作增加 idempotent 或nonmutating 限定符。就对应的代理方法的签名而言,idempotent或nonmutating没有任何效果。例如,考虑下面的接口:
interface Example { string op1(); idempotent string op2(); nonmutating string op3(); }; 这个接口的代理类是: public interface ExamplePrx : Ice.ObjectPrx { String op1(); String op2(); String op3(); }
  因为idempotent和nonmutating影响的是调用分派(call dispatch )、而不是接口的某个方面,这三个方法看起来一样,是有道理的。

  参数传递

  in 参数

  C# 映射的参数传递规则非常简单:参数或者通过值传递 (对于值类型),或者通过引用传递 (对于引用类型)。在语义上,这两种传递参数的方式是等价的:调用保证不会改变参数的值 (XREF 给出了一些警告)。
  下面的接口有一些操作,会把各种类型的参数从客户传给服务器:
struct NumberAndString { int x; string str; }; sequence<string> StringSeq; dictionary<long, StringSeq> StringTable; interface ClientToServer { void op1(int i, float f, bool b, string s); void op2(NumberAndString ns, StringSeq ss, StringTable st); void op3(ClientToServer* proxy); }; Slice 编译器为这个定义生成这样的代码: public interface ClientToServerPrx : Ice.ObjectPrx { public void op1(int i, float f, boolean b, String s); public void op2(NumberAndString ns, StringSeqss, StringTable st); public void op3(ClientToServerPrx proxy); } 假定我们有一个代理,指向的是ClientToServer接口,客户代码可以这样传递参数: ClientToServerPrx p = ...; // Get proxy... p.op1(42, 3.14f, true, "Hello world!"); // Pass simple literals int i = 42; float f = 3.14f; boolean b = true; String s = "Hello world!"; p.op1(i, f, b, s); // Pass simple variables NumberAndString ns = new NumberAndString(); ns.x = 42; ns.str = "The Answer"; StringSS ss = new StringSS(): ss.Add(“hello world”); StringTable st = new StringTable(); st[0] = ns; st.put(new Long(0), ns); p.op2(ns, ss, st); // Pass complex variables p.op3(p); // Pass proxy
0
相关文章