C#bond 中都有哪些可用的数据类型?

Posted

技术标签:

【中文标题】C#bond 中都有哪些可用的数据类型?【英文标题】:What are the available datatypes in C# bond?C#bond 中有哪些可用的数据类型? 【发布时间】:2018-01-11 15:05:40 【问题描述】:

我想知道用 C# 键格式表示应该包含这些字段的表的最佳方法是什么?

字符串名字 字符串姓氏 字符串电子邮件 bool 已注册 日期时间日期加入 字符性别 列表收藏夹 字符串城市 字符串状态 unit16 邮编 字符串国家 列表FrequentPagesURLs

而且我想要一些类似于那种格式的东西

namespace MyProject

    struct Key
    
        0: required string Email;
    

    struct Value
    
        0: required string FirstName; 
        1: optional char Gender;
        .
        .
        .
    

我不确定用 C# 键格式表示 charDateTimeList<string> 以在对象存储中创建表时使用它们的最佳方式是什么。

【问题讨论】:

【参考方案1】:

根据Bond的官方文档,有以下几种:

基本类型:bool、uint8、uint16、uint32、uint64、int8、int16、int32、int64、float、double、string、wstring。

容器:blob、列表、向量、集合、映射、可为空。

用户定义类型:枚举、结构或绑定,其中 T 是结构。

但是,如果您使用bond 生成C# 代码,该文档还解释了如何生成DateTime、char 等。即在您的 CLI 命令中使用以下内容:

gbc c# --using="DateTime=System.DateTime" date_time.bond

using 参数是您放置类型别名的位置,例如“char=System.Char;DateTime=System.DateTime”。

我不知道这是否对您有足够的帮助,如果您需要其他任何东西,请告诉我。

来源:

https://microsoft.github.io/bond/manual/compiler.html

https://microsoft.github.io/bond/manual/bond_cs.html

【讨论】:

我对生成DateTime、char等的部分很困惑。我不确定如何以我上面提到的格式将其写入架构文件中。如果您能提供我需要创建的表的示例,那就太好了。 存在一个关于how to represent TimeSpan 的问题,指向example about DateTime【参考方案2】:

我会将性别字段建模为枚举,因为这比字符更明确; DateTime 字段为uint64,但使用a type converter 将其转换为a DateTime struct in C#;和 List<string> 字段作为 vector<string>:

namespace MyProject;

using DateTime=uint64;

enum Gender

    Unspecified;
    ...


struct Favorite  ... 
struct FrequentPagesURL  ... 

struct SomeType

    ...
    7: DateTime DateJoined;
    8: Gender Gender = Unspecified;
    9: vector<Favorite> Favorites;
    ...
    17: vector<FrequentPagesURL> FrequentPagesURLs;
    ...

您可能需要考虑将 DateJoined 字段建模为 string/blob 并使用类型转换器将其转换为 C# 中的 DateTimeOffset struct depending on your needs。

【讨论】:

以上是关于C#bond 中都有哪些可用的数据类型?的主要内容,如果未能解决你的问题,请参考以下文章

C语言中的“货币类型”用啥表示?在程序中都有哪些书写形式?越全越好...

c++中都有哪些类类型

C语言中都有哪些存储类型?

C语言中标识符的命名规则是怎样的?C语言中都有哪些常见的数据类型?

java中都有哪些数据结构

python语言中都有哪些数据类型