Wednesday, 15 July 2015

arrays - C# Marshalling vs Serialization -


I have a C # app that reads / writes messages from some predefined protocols / USB, so I have at least There are 2 options i is a serialize / deserill is second martial. In the beginning I was picking the first option and it worked fine. But after presenting more and more message types, I am annoyed with the messy implementation of every message type and feel that Marshall may be a better way I have not used Marshal before, is this the right way? I did some tests I have a problem when I have a structure with an array, how to write it? In the form of the following: - [Layout layouts (layout kickind, size = totalbest infrastructures), serializable] Public Straight Samadata {/// Four [15] [MarshalAsTribut (Unmanaged Type Bell) Valettsrat, size = 15)] public sbyte [] data; /// at [15] [MarshallAsTribute (unmanaged type. BevalArere, Siegenset = 15)] Public Ink [] Problem; }

It appears that both the data and the problem are references and I have a new audience to use them, which does not look right for me I need a whole structure The continuous block of which I do not think the new operator will do this for me.

Any suggestion is really appreciated

The following is how done by the serialization.

  list & lt; Byte & gt; RawData = new list & lt; Byte & gt; (); RawData.AddRange (BitConverter.GetBytes (ProtocolVersion)); Operator ID byte / 16 byte [] temp = new byte [16]; Array.Copy (encoding. ASCIIGetBytes (OperatorID), Temporary, OperatorID, Closing & gt; Templant.Temple.line: OperatorID.LINE); RawData.AddRange (Temporary); // Array for 16 bytes operator password Clear (temp, 0, temp.Length); Array.Copy (Encoding. ASCIIGetBytes (Operator Password), Temporary, Operator password.Lyd> temp.Length? Temp.Length: Operator password. Length); RawData.AddRange (Temporary);   

The following is done by Marshall

  Static byte [] rationalialize & lt; T & gt; (T OBJ) {if (obj == null) {return tap; } Int size = Marshal.SizeOf (obj); Byte [] results = new byte [size]; IntPtr Buffer = Marshall Elcohygable (size); Martial Structure oct print (obje, buffer, false); Martial Copy (buffer, result, 0, size); Return result; It seems that I just need an array like the following and it works.    



No comments:

Post a Comment