(oops, ignore the double-click!)
From ProtoContract, I'm taking this to be a protobuf-net question.
No, it doesn't currently offer a way to serialize structs; the main
reason for this being that structs should pretty-much always be
immutable, which makes a reflection-based API (such as protobuf-net)
very tricky.It could *perhaps* be possible to automate something if
there is always a constructor with parameter names/types a 100% match
to the designated members, but it isn't something I've prioritised.
Possible workarounds at the moment:
- use a class
- use a private serialization member that returns a calss-based DTO
for your data
- use private serialization members that represent your struct
For an example of the second (although I'd need to check whether it
calls the setter when a non-null object is found):
struct Foo {
public Foo(string name) {this.name = name;}
private readonly string name;
public string Name {get {return name;}}
}
[ProtoContract]
class FooDto {
[ProtoMember(1)]
public string Name {get;set;}
}
[ProtoContract]
class MyMessage {
public Foo SomeValue {get;set;}
[ProtoMember(1, Name=SomeValue)]
private FooDto SomeValueDto {
get { return new FooDto { Name = SomeValue.Name }; }
set {
if(value == null) { /* up to you... */ }
else { SomeValue = new Foo(value.Name); }
}
}
}
Or an example of the third bullet:
[ProtoContract]
class MyMessage {
public Foo SomeValue {get;set;}
[ProtoMember(1, Name=SomeValueName)]
private string SomeValueName {
get { return SomeValue.Name; }
set { SomeValue = new Foo(value); /* set all other vals
etc*/ }
}
}
Beyond that, nothing at the moment. Simply: custom structs don't
happen anywhere *near* as much as classes, and due to their nature
they are a bit of a pain to deal with in code, unless you are using
code-gen to create them.
Marc
On Aug 18, 3:43 am, martin martindev...@gmail.com wrote:
It can't be done, which is a shame, is there a particular reason for
this and is there some way around it? (besides the obvious one of
turning my struct into a class)
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups
Protocol Buffers group.
To post to this group, send email to protobuf@googlegroups.com
To unsubscribe from this group, send email to
protobuf+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/protobuf?hl=en
-~--~~~~--~~--~--~---