Wednesday 15 February 2012

serialization - How can i serialize a FileStream in c#? -



serialization - How can i serialize a FileStream in c#? -

i hope not repeated question. i've class :

[serializable] class myclass { int type; list <filestream> listfile; string content_text; public myclass(int t) { type = t; } public myclass() { type = 0; } }

i need send object of myclass in socket method socket.send(byte []). i've serialize object. if add together [serializable], filestream isn't serializable, , exception runtime. can help me ? give thanks much.

when add together serializable class doesn't alter anything, tells clr class can serialized.

beacuse of this, classes part of object , not default serializable not changed in anyway, , such cause exception see when effort to.

the next link show msdn documentation serializable attribute : http://msdn.microsoft.com/en-us/library/system.serializableattribute(v=vs.110).aspx

you can mark individual properties in class not serializable using 'nonserializedattribute' follows:

[serializable] class myclass { int type; [nonserialized] list<filestream> listfile; string content_text; public myclass(int t) { type = t; } public myclass() { type = 0; } }

this prevent exception occurring, removing actual property serialized output, means if wanting pass filestream objects on socket, fresh out of luck unfortunately because not going able to.

now, said, can effort read contents of file stream(s) byte arrays, , send byte arrays 'byte[]' serializable without issues.

i recommend if going start sending byte[] arrays of arbitrary length on socket, @ using binary streaming protocol (perhaps google proto buffers) rather default text serialized objects out of box.

c# serialization filestream

No comments:

Post a Comment