问题
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