Click here to Skip to main content
15,888,984 members
Home / Discussions / C#
   

C#

 
QuestionHow to convert any object to byte[] without marking it Serializable or DataContract? Pin
1eyhk117-Aug-10 16:35
1eyhk117-Aug-10 16:35 
AnswerRe: How to convert any object to byte[] without marking it Serializable or DataContract? Pin
Gonzalo Cao17-Aug-10 20:56
Gonzalo Cao17-Aug-10 20:56 
GeneralRe: How to convert any object to byte[] without marking it Serializable or DataContract? Pin
1eyhk118-Aug-10 1:51
1eyhk118-Aug-10 1:51 
GeneralReverse Pin
1eyhk118-Aug-10 16:19
1eyhk118-Aug-10 16:19 
GeneralRe: Reverse Pin
Gonzalo Cao18-Aug-10 20:18
Gonzalo Cao18-Aug-10 20:18 
GeneralRe: Reverse Pin
1eyhk118-Aug-10 21:43
1eyhk118-Aug-10 21:43 
GeneralRe: Reverse Pin
Gonzalo Cao18-Aug-10 21:44
Gonzalo Cao18-Aug-10 21:44 
GeneralWokring now, atttempts: from interop to BinaryFormatter to DataContractSerializer - still need to see MyAccount[] Pin
1eyhk120-Aug-10 19:33
1eyhk120-Aug-10 19:33 
Oh bummer
<br />
{"Type 'XXXXX.MyAccount' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed."}<br />


I suspect this is because MyAccount is actually a complex hierarchy containing a child collection...
<br />
class MyAccount<br />
{<br />
...<br />
public List&lt;Invoicesgt; GroupList;<br />
}<br />

Eekkk... I was going to wire down an array of accounts (i.e. MyAccount[]) but now can't even wire down one single MyAccount... I desparation I tried this:
<br />
 using (System.IO.MemoryStream stream = new System.IO.MemoryStream())<br />
{<br />
	BinaryFormatter formatter = new BinaryFormatter();<br />
	formatter.Serialize(stream, result);<br />
	byte[] BinAcct = stream.ToArray());<br />
}<br />


But even then I ran into runtime exception, complaint was that class is not marked serializable (Although marked DataContract), looks like I got lucky with
<br />
DataContractSerializer dataContractSerializer = new DataContractSerializer(Acct.GetType());<br />
using (MemoryStream memoryStream = new MemoryStream())<br />
{<br />
dataContractSerializer.WriteObject(memoryStream, Acct);<br />
btAccountSet = new byte[memoryStream.Length];<br />
Array.Copy(memoryStream.GetBuffer(), btAccountSet, btAccountSet.Length);<br />
}<br />

This finally get the job done (only serialization part, still stuck during deserialization).
<br />
1. Serialize side:<br />
DataContractSerializer serial = new DataContractSerializer(Acct.GetType());<br />
using (MemoryStream memoryStream = new MemoryStream())<br />
{<br />
	serial.WriteObject(memoryStream, oAcct);<br />
	btPayload = new byte[memoryStream.Length];<br />
	Array.Copy(memoryStream.GetBuffer(), btPayload, btPayload.Length);<br />
}<br />
                <br />
2. Deserialize side:			<br />
byte[] raw = (byte[]) GetPayload();<br />
serial = new DataContractSerializer(typeof(MyAccount));<br />
using (MemoryStream memoryStream = new MemoryStream(raw))<br />
{<br />
	memoryStream.Seek(0, SeekOrigin.Begin);<br />
	oAcct = (MyAccount) serial.ReadObject(memoryStream);<br />
}<br />


So, all good sending a single object, but still need to attempt sending/serializing a list of MyAccount (with subclasses)

Thanks

modified on Saturday, August 21, 2010 1:46 AM

GeneralRe: Wokring now, atttempts: from interop to BinaryFormatter to DataContractSerializer - still need to see MyAccount[] Pin
Gonzalo Cao23-Aug-10 21:50
Gonzalo Cao23-Aug-10 21:50 
GeneralRe: Wokring now, atttempts: from interop to BinaryFormatter to DataContractSerializer - still need to see MyAccount[] Pin
1eyhk123-Aug-10 21:58
1eyhk123-Aug-10 21:58 
Questionhow to combine audio file with video file Pin
yftah198917-Aug-10 9:56
yftah198917-Aug-10 9:56 
AnswerRe: how to combine audio file with video file Pin
Ravi Bhavnani17-Aug-10 10:38
professionalRavi Bhavnani17-Aug-10 10:38 
QuestionGPS Location Pin
Bravegee17-Aug-10 6:48
Bravegee17-Aug-10 6:48 
AnswerRe: GPS Location Pin
OriginalGriff17-Aug-10 8:16
mveOriginalGriff17-Aug-10 8:16 
GeneralRe: GPS Location Pin
Eduard Keilholz17-Aug-10 21:34
Eduard Keilholz17-Aug-10 21:34 
GeneralRe: GPS Location Pin
Bravegees18-Aug-10 0:03
Bravegees18-Aug-10 0:03 
GeneralRe: GPS Location Pin
Bravegees18-Aug-10 0:05
Bravegees18-Aug-10 0:05 
QuestionAnonymous delegates Pin
RugbyLeague17-Aug-10 4:41
RugbyLeague17-Aug-10 4:41 
AnswerRe: Anonymous delegates Pin
OriginalGriff17-Aug-10 4:53
mveOriginalGriff17-Aug-10 4:53 
GeneralRe: Anonymous delegates Pin
RugbyLeague17-Aug-10 4:57
RugbyLeague17-Aug-10 4:57 
GeneralRe: Anonymous delegates Pin
OriginalGriff17-Aug-10 5:00
mveOriginalGriff17-Aug-10 5:00 
GeneralRe: Anonymous delegates Pin
RugbyLeague17-Aug-10 5:04
RugbyLeague17-Aug-10 5:04 
GeneralRe: Anonymous delegates Pin
OriginalGriff17-Aug-10 6:20
mveOriginalGriff17-Aug-10 6:20 
JokeRe: Anonymous delegates Pin
Not Active17-Aug-10 6:30
mentorNot Active17-Aug-10 6:30 
GeneralRe: Anonymous delegates Pin
Yusuf17-Aug-10 13:09
Yusuf17-Aug-10 13:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.