How to convert any object to byte[] without marking it Serializable or DataContract?
-
hello How to convert any object to byte[] without marking it Serializable or DataContract? Of course this would work but you'd need to mark myObj class Serializable (I'm lazy dont want handcode serializable for big classes sorry) using (System.IO.MemoryStream returnStm = new System.IO.MemoryStream()) { BinaryFormatter ftm = new BinaryFormatter(); ftm.Serialize(returnStm , myObj); return returnStm .ToArray(); // this would return byte[] } Anyone who can tell me how I can commit such crime? Thank you
-
hello How to convert any object to byte[] without marking it Serializable or DataContract? Of course this would work but you'd need to mark myObj class Serializable (I'm lazy dont want handcode serializable for big classes sorry) using (System.IO.MemoryStream returnStm = new System.IO.MemoryStream()) { BinaryFormatter ftm = new BinaryFormatter(); ftm.Serialize(returnStm , myObj); return returnStm .ToArray(); // this would return byte[] } Anyone who can tell me how I can commit such crime? Thank you
Hi, I do this and it works for me. public byte[] RawSerialize(object item, Type anyType) { int structSize = Marshal.SizeOf(item); IntPtr ptr = Marshal.AllocHGlobal(structSize); Marshal.StructureToPtr(item, ptr, true); byte[] bff = new byte[structSize]; Marshal.Copy(ptr, bff, 0, structSize); Marshal.FreeHGlobal(ptr); return bff; }
-
Hi, I do this and it works for me. public byte[] RawSerialize(object item, Type anyType) { int structSize = Marshal.SizeOf(item); IntPtr ptr = Marshal.AllocHGlobal(structSize); Marshal.StructureToPtr(item, ptr, true); byte[] bff = new byte[structSize]; Marshal.Copy(ptr, bff, 0, structSize); Marshal.FreeHGlobal(ptr); return bff; }
-
Hi, I do this and it works for me. public byte[] RawSerialize(object item, Type anyType) { int structSize = Marshal.SizeOf(item); IntPtr ptr = Marshal.AllocHGlobal(structSize); Marshal.StructureToPtr(item, ptr, true); byte[] bff = new byte[structSize]; Marshal.Copy(ptr, bff, 0, structSize); Marshal.FreeHGlobal(ptr); return bff; }
-
public static object RawDeserialize(byte[] rawdatas, Type anytype)
{
int rawsize = Marshal.SizeOf(anytype);if (rawsize != rawdatas.Length) { return null; } IntPtr buffer = Marshal.AllocHGlobal(rawsize); Marshal.Copy(rawdatas, 0, buffer, rawsize); object retobj = Marshal.PtrToStructure(buffer, anytype); Marshal.FreeHGlobal(buffer); return retobj; }
Here it is :)
-
public static object RawDeserialize(byte[] rawdatas, Type anytype)
{
int rawsize = Marshal.SizeOf(anytype);if (rawsize != rawdatas.Length) { return null; } IntPtr buffer = Marshal.AllocHGlobal(rawsize); Marshal.Copy(rawdatas, 0, buffer, rawsize); object retobj = Marshal.PtrToStructure(buffer, anytype); Marshal.FreeHGlobal(buffer); return retobj; }
Here it is :)
-
Many thanks Master Cao! If I still can't get my WCF contract working I may just try serialize/de-serialize whole deal to byte[]
You welcome. Good luck with you project.
-
You welcome. Good luck with you project.
Oh bummer
{"Type 'XXXXX.MyAccount' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed."}
I suspect this is because MyAccount is actually a complex hierarchy containing a child collection...class MyAccount { ... public List<Invoicesgt; GroupList; }
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:using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, result); byte[] BinAcct = stream.ToArray()); }
But even then I ran into runtime exception, complaint was that class is not marked serializable (Although marked DataContract), looks like I got lucky withDataContractSerializer dataContractSerializer = new DataContractSerializer(Acct.GetType()); using (MemoryStream memoryStream = new MemoryStream()) { dataContractSerializer.WriteObject(memoryStream, Acct); btAccountSet = new byte[memoryStream.Length]; Array.Copy(memoryStream.GetBuffer(), btAccountSet, btAccountSet.Length); }
This finally get the job done (only serialization part, still stuck during deserialization).1. Serialize side: DataContractSerializer serial = new DataContractSerializer(Acct.GetType()); using (MemoryStream memoryStream = new MemoryStream()) { serial.WriteObject(memoryStream, oAcct); btPayload = new byte[memoryStream.Length]; Array.Copy(memoryStream.GetBuffer(), btPayload, btPayload.Length); } 2. Deserialize side: byte[] raw = (byte[]) GetPayload(); serial = new DataContractSerializer(typeof(MyAccount)); using (MemoryStream memoryStream = new MemoryStream(raw)) { memoryStream.Seek(0, SeekOrigin.Begin); oAcct = (MyAccount) serial.ReadObject(memoryStream); }
So, all good sending a single object, but still need to attempt sending/serializing a list of MyAccount (with subclasses) Thanksmodified on Saturday, August 21, 2010 1:46 AM
-
Oh bummer
{"Type 'XXXXX.MyAccount' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed."}
I suspect this is because MyAccount is actually a complex hierarchy containing a child collection...class MyAccount { ... public List<Invoicesgt; GroupList; }
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:using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, result); byte[] BinAcct = stream.ToArray()); }
But even then I ran into runtime exception, complaint was that class is not marked serializable (Although marked DataContract), looks like I got lucky withDataContractSerializer dataContractSerializer = new DataContractSerializer(Acct.GetType()); using (MemoryStream memoryStream = new MemoryStream()) { dataContractSerializer.WriteObject(memoryStream, Acct); btAccountSet = new byte[memoryStream.Length]; Array.Copy(memoryStream.GetBuffer(), btAccountSet, btAccountSet.Length); }
This finally get the job done (only serialization part, still stuck during deserialization).1. Serialize side: DataContractSerializer serial = new DataContractSerializer(Acct.GetType()); using (MemoryStream memoryStream = new MemoryStream()) { serial.WriteObject(memoryStream, oAcct); btPayload = new byte[memoryStream.Length]; Array.Copy(memoryStream.GetBuffer(), btPayload, btPayload.Length); } 2. Deserialize side: byte[] raw = (byte[]) GetPayload(); serial = new DataContractSerializer(typeof(MyAccount)); using (MemoryStream memoryStream = new MemoryStream(raw)) { memoryStream.Seek(0, SeekOrigin.Begin); oAcct = (MyAccount) serial.ReadObject(memoryStream); }
So, all good sending a single object, but still need to attempt sending/serializing a list of MyAccount (with subclasses) Thanksmodified on Saturday, August 21, 2010 1:46 AM
Were you able to fix it?? With the sample code you've posted I was not able to find the problem.
-
Were you able to fix it?? With the sample code you've posted I was not able to find the problem.