C# Bond:字符串到 wstring

C# Bond: string to wstring

Bond C# manual 中,注释如下:

These following changes will break wire compatibility and are not recommended:

  • Adding or removing required fields
  • Incompatible change of field types (any type change not covered above); e.g.: int32 to string, string to wstring
  • ...

但是并没有解释为什么。这里的用例是我正在使用将 C# 应用程序与 C++ 后端连接起来的 Bond。该字段当前是一个字符串。我想将它更改为 wstring。手册指出 C# 字符串可以处理 C++ 字符串和 C++ wstrings。因此,为什么我不能将字段类型从字符串更改为 wstring?为什么这个断线兼容?

在 Bond 的二进制格式中,字符串是 UTF8 编码的(无 BOM),而 wstrings 是 UTF16-LE 编码的。如果您要将字段从字符串切换为 wstring,读取端会尝试将 UTF8 数据解释为 UTF16-LE 数据。这两种编码彼此不兼容,因此将字段类型从 string 更改为 wstring 是一个重大更改。

请注意,手册说 "For example C# string can represent either Bond type string or wstring." 它没有说明任何有关 C++ 类型的信息。在 C# 和 C++ 中使用 Bond 时,有 三种 类型系统:Bond、C# 和 C++。

如果在 C++ 方面,您想使用类似于 std::wstring 的方法将字段存储在内存中,请看一下使用 Custom type mapping with the string concept.