Protobuf的Enum枚举类型不能同名?

Protobuf的Enum枚举类型不能同名?

报错

enum CHARACTER_STATE
{
    IDLE = 0;
    MOVE = 1;
}

enum ENTITY_EVENT
{
    NONE = 0;
    IDLE = 1;
    MOVE_FWD = 2;
    MOVE_BACK = 3;
    JUMP = 4;
}

"message": "\"IDLE\" is already defined in \"SkillBridge.Message\".


"message": "Note that enum values use C++ scoping rules, meaning that enum values are siblings of their type, not children of it.  Therefore, \"NONE\" must be unique within \"SkillBridge.Message\", not just within \"ENTITY_EVENT\".",

原因

protobuf使用类似c的枚举规则,不允许枚举中出现同名。

解决办法

给枚举项加入前缀

enum CHARACTER_STATE
{
    IDLE = 0;
    MOVE = 1;
}


enum ENTITYEVENT
{
    ENTITYEVENT_NONE =0;
    ENTITY_EVENT_IDLE = 1;
    ENT_ITY_EVENT_MOVE_FWD = 2;
    ENT_IT_Y_EVENT_MOVE_BACK = 3;
    ENT_IT_Y_EVENT_JUM_P = 4;
}

在生成C#代码时,会忽略掉等同于ENTITYEVENT的前缀:

  public enum CHARACTER_STATE {
    [pbr::OriginalName("IDLE")] Idle = 0,
    [pbr::OriginalName("MOVE")] Move = 1,
  }

  public enum ENTITYEVENT {
    [pbr::OriginalName("ENTITYEVENT_NONE")] None = 0,
    [pbr::OriginalName("ENTITY_EVENT_IDLE")] Idle = 1,
    [pbr::OriginalName("ENT_ITY_EVENT_MOVE_FWD")] MoveFwd = 2,
    [pbr::OriginalName("ENT_IT_Y_EVENT_MOVE_BACK")] MoveBack = 3,
    [pbr::OriginalName("ENT_IT_Y_EVENT_JUM_P")] JumP = 4,
  }
上一篇:小程序搜索优化指南(SEO)


下一篇:微信公众号接口生成菜单(链接、文本回复及图文消息)