views:

60

answers:

2

I have a problem with serialisation. The class in question represents a network packet and has an associated byte array for payload, which may be null.

I exposed this using GetData and SetData methods in line with Microsoft Framework Design Guidelines recommendations, because in order to prevent bizarre crosstalk, reading and writing the array entails copying it rather than passing a reference. The guidelines suggest Get/Set when there are significant effects such as object creation, and that's what I did.

Unfortunately, this has had the consequence that the internal member is not serialised. The member is marked protected. Apart from exposing it as a public property, how can I cause this to be serialised when the object is passed from server to client by a web service?

A: 

How about binary serialization?

ggf31416
It's already using binary serialisation. The binding is netTcp which IIRC uses binary encoding by default.
Peter Wone
+3  A: 

(edit: assumes web usage; protocol not clear in original question)

Can you use WCF? The DataContractSerializer supports non-public members:

[DataContract]
class Foo {
   [DataMember] // can also be used on a field - not just properties
   private int Bar {get;set;} // or internal or protected
}

If not, you'll have to simply consider it a DTO, and make the property public.

Other options - binary serialization? BinaryFormatter is not portable between platforms (so not ideal for all web services), but things like protocol buffers are more friendly. But there is no WDSL support for this, nor any IPC stack (it is raw data only).

Marc Gravell
Can you do that? (Put DataMember on a private member) I was just setting up to try that! - and it WORKS! HURRAH! YOU GET THE POINTS!
Peter Wone