How to map System Enum's in Protobuf.Net

£可爱£侵袭症+ 提交于 2019-12-12 13:20:53

问题


I have a class which has a property of type System.IO.FileAttribute (enum)

Upon serializing with protobuf-net I get the error:

No wire-value is mapped to the enum System.IO.FileAttributes.Hidden, System, Archive

How would I go about mapping system enum's to a Contract with Members?


回答1:


That is a [Flags] enum, which doesn't really have a direct map in protobuf (as defined by google). I would simply re-expose as int:

public FileAttributes Attributes {get;set;}

[ProtoMember(12)] // whavever
private int AttributesSerialized {
    get { return (int)Attributes; }
    set { Attributes = (FileAttributes)value; }
}

Additionally, IIRC, I have already coded v2 to operate this way on [Flags] automatically, and to optionally allow pass-thru of enums (to treat as the underlying value automatically).



来源:https://stackoverflow.com/questions/5204324/how-to-map-system-enums-in-protobuf-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!