ProtoContract applied to structs

369 views
Skip to first unread message

martin

unread,
Aug 17, 2009, 10:43:04 PM8/17/09
to Protocol Buffers
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)

Marc Gravell

unread,
Aug 18, 2009, 12:07:00 AM8/18/09
to Protocol Buffers

Marc Gravell

unread,
Aug 18, 2009, 12:23:49 AM8/18/09
to Protocol Buffers
(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:

martin

unread,
Aug 18, 2009, 10:57:10 AM8/18/09
to Protocol Buffers
Damn that's a shame, I need to minimise allocations in my current
system (it's networking for games, and I eventually plan to port it to
the Xbox using XNA, and the xbox GC isn't so great)

THanks for the help Marc

Marc Gravell

unread,
Aug 18, 2009, 3:19:46 PM8/18/09
to Protocol Buffers
Part of the problem here is that I can't think of a clean way to do it
that doesn't go mad with boxing, or losing struct changes. I'm
guessing that for xna you have mutable structs, so immutability isn't
necessarily an issue, but then I need lots of "ref" instead... Fun...

Also; note that the CF version has some... "quirks" at the moment for
complex models; there is a plan to address this, but it is a lot of
work. The problem seems to be in inherent limitation of CF and
generics.

Marc
Reply all
Reply to author
Forward
0 new messages