17.9.1 Unary operators

本文介绍了C#中一元运算符声明的规则,包括+、-、!、~、++、--、true和false等运算符的参数类型及返回类型的要求,并通过一个整数向量类的例子展示了如何实现和使用++运算符。
The following rules apply to unary operator declarations, where T denotes
the class or struct type that contains
the operator declaration:
? A unary +, -, !, or ~ operator must take a single parameter of type T and
can return any type.
? A unary ++ or -- operator must take a single parameter of type T and must
return type T.
? A unary true or false operator must take a single parameter of type T and
must return type bool.
The signature of a unary operator consists of the operator token (+, -, !,
~, ++, --, true, or false) and the type
of the single formal parameter. The return type is not part of a unary
operator?s signature, nor is the name of the
formal parameter.
C# LANGUAGE SPECIFICATION
256
The true and false unary operators require pair-wise declaration. A
compile-time error occurs if a class
declares one of these operators without also declaring the other. The true
and false operators are described
further in §14.16.
[Example: The following example shows an implementation and subsequent
usage of operator++ for an integer
vector class:
public class IntVector
{
public int Length { ? } // read-only property
public int this[int index] { ? } // read-write indexer
public IntVector(int vectorLength) { ? }
public static IntVector operator++(IntVector iv) {
IntVector temp = new IntVector(iv.Length);
for (int i = 0; i < iv.Length; ++i)
temp[i] = iv[i] + 1;
return temp;
}
}
class Test
{
static void Main() {
IntVector iv1 = new IntVector(4); // vector of 4x0
IntVector iv2;
iv2 = iv1++; // iv2 contains 4x0, iv1 contains 4x1
iv2 = ++iv1; // iv2 contains 4x2, iv1 contains 4x2
}
}
Note how the operator method returns the value produced by adding 1 to the
operand, just like the postfix
increment and decrement operators(§14.5.9), and the prefix increment and
decrement operators (§14.6.5). Unlike
in C++, this method need not, and, in fact, must not, modify the value of
its operand directly. end example]
### gRPC 中 `channel.unary_unary` 方法详解 在 gRPC 的实现中,`channel.unary_unary` 是一种方法调用模式,用于描述客户端向服务器发送一个请求并接收一个响应的简单 RPC 调用。这种模式是最常见的 RPC 调用形式之一,适用于大多数需要单次请求和单次响应的场景[^1]。 #### 1. `unary_unary` 的含义 `unary_unary` 表示的是 gRPC 的四种基本通信模式之一: - **Unary RPC**:客户端发送一个请求,服务器返回一个响应。 - 在 gRPC 的术语中,“unary” 指的是单一的消息传递模式,因此 `unary_unary` 表示客户端发送一个消息(unary),服务器也返回一个消息(unary)。 具体来说,`channel.unary_unary` 是 gRPC Python 客户端中的一种方法调用方式,它直接封装了 unary-unary 模式的逻辑[^4]。 #### 2. `channel.unary_unary` 的用法 在 gRPC Python 中,`channel.unary_unary` 方法通常用于动态地调用远程服务的方法,而不需要显式地生成客户端代码。以下是其基本用法: ```python import grpc # 创建一个通道 channel = grpc.insecure_channel(&#39;localhost:50051&#39;) # 获取 unary_unary 方法 stub_method = channel.unary_unary( &#39;/test.Bilibili/HelloABiao&#39;, # 完整的服务方法路径 request_serializer=test.HelloABiaoRequest.SerializeToString, # 请求序列化器 response_deserializer=test.HelloABiaoResponse.FromString # 响应反序列化器 ) # 构造请求对象 request = test.HelloABiaoRequest(name="Alice", age=25) # 调用远程方法 response = stub_method(request) # 输出响应 print(response.result) ``` #### 参数说明 - **方法路径**:`&#39;/test.Bilibili/HelloABiao&#39;` 是完整的 gRPC 方法路径,格式为 `/<package>.<service>/<method>`。 - **request_serializer**:指定如何将请求对象序列化为字节流。通常使用 Protocol Buffers 提供的 `SerializeToString` 方法。 - **response_deserializer**:指定如何将字节流反序列化为响应对象。通常使用 Protocol Buffers 提供的 `FromString` 方法。 #### 3. 示例代码 以下是一个完整的 unary-unary 模式示例,基于 `helloworld.proto` 文件: ```proto syntax = "proto3"; package helloworld; service Greeter { rpc SayHello (HelloRequest) returns (HelloResponse); } message HelloRequest { string name = 1; } message HelloResponse { string message = 1; } ``` 编译后生成 Python 代码,并使用 `channel.unary_unary` 调用: ```python import grpc import helloworld_pb2 # 创建通道 channel = grpc.insecure_channel(&#39;localhost:50051&#39;) # 使用 channel.unary_unary 动态调用 stub_method = channel.unary_unary( &#39;/helloworld.Greeter/SayHello&#39;, request_serializer=helloworld_pb2.HelloRequest.SerializeToString, response_deserializer=helloworld_pb2.HelloResponse.FromString ) # 构造请求 request = helloworld_pb2.HelloRequest(name="World") # 发起调用 response = stub_method(request) # 打印响应 print(response.message) ``` #### 4. 注意事项 - 确保 `request_serializer` 和 `response_deserializer` 的实现与 `.proto` 文件定义一致。 - 如果服务端未正确实现指定的方法,可能会抛出 `grpc.RpcError` 异常。 - `channel.unary_unary` 更适合动态调用场景,对于静态调用,推荐使用 `grpc_tools.protoc` 生成的客户端代码[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值