there is a certain class that needs to serialize, i.e. Get an array byte in a variable. What ways can this be done?, I tried to write using the ISERIALIZABLE interface. Are there any other ways?
For example class:
class ctestmessageclass
{
Public int A {Get; SET; }
Public Double B {Get; SET; }
Public String C {Get; SET; }
Private INT64 D {Get; SET; }
}
Answer 1, Authority 100%
Use the built-in serialization. To do this, it is enough to give the class attribute Serializable
:
[serializable]
Class Testmessage.
{
Public int A {Get; SET; }
Public Double B {Get; SET; }
Public String C {Get; SET; }
Private INT64 D {Get; SET; }
}
If you really need binary serialization (meaning potential problems when working in a heterogeneous system), use BINARYFORMATTER
:
class program
{
Static Void Main (String [] Args)
{
TestMessage TM1 = ...;
Var Stream = Save (TM1);
stream.seek (0, seekorigin.begin);
var TM2 = Load (Stream);
}
Static Stream Save (TestMessage TM)
{
var formatter = new binaryFormatter ();
Var Stream = New MemoryStream ();
Formatter.Serialize (Stream, TM);
Return Stream;
}
Static Testmessage Load (Stream S)
{
var formatter = new binaryFormatter ();
var tm = (testmessage) formatter.deserialize (s);
Return TM;
}
}
Alternatively, you can use XML serialization, but note that it “from the box” ignores private properties, so it will be necessary to either implement iSerializable
/ ixmlserializable
or Use DataContractSerializer
Instead of Xmlserializer
.
Answer 2, AUTHORITY 60%
I used json.net for serialization and remained very pleased!
Your example will look like this:
using newtonsoft.json;
using newtonsoft.json.converters;
[Jsonobject (memberserialization.optin)]
[JSONConverter (Typeof (CTESTMESSACLASSCONVERTER)]
Class CTESTMESSACLASS.
{
[Jsonproperty]
Public int A {Get; SET; }
[Jsonproperty]
Public Double B {Get; SET; }
[Jsonproperty]
Public String C {Get; SET; }
[Jsonproperty]
Private INT64 D {Get; SET; }
}
// json converter
Public Class CTESTMESSACLASSCONVERTER: CustomCreationConverter & LT; CTESTMESSACLASS & GT;
{
Public Override CTESTMESSACLASS CREATE (Type ObjectType)
{
Return new ctestmessageclass ();
}
}
Serialization:
String S = JSONConvert.SerializeObject (TestMessage);
// Reseserialization
CTESTMESSACLASS CD = JSONConvert.DeserializeObject & lt; CTESTMESSACLASS & GT; (z);
Answer 3, Authority 40%
Serialize in XML .
Answer 4, Authority 20%
If you need fast serialization, you will have to abandon the full implementation.
In your case it may look like this:
interface iSupportsbinarySerialization
{
Void Serialize (Out byte [] Buffer);
Void Serialize (BinaryWriter Writer);
Void Deserialize (BYTE [] Buffer);
Void Deserialize (BinaryReader Reader);
}
Class TestmessageClass: ISUPPORTSBINARYSIALIZATION
{
Public int A.
{
get;
SET;
}
Public Double B.
{
get;
SET;
}
Public String C.
{
get;
SET;
}
Private Long D.
{
get;
SET;
}
Public Void Serialize (Out byte [] Buffer)
{
VAR Size = 0
+ Sizeof (int)
+ Sizeof (Double)
+ sizeof (int) + sizeof (char) * C.Length
+ sizeof (long);
buffer = new byte [size];
var stream = new MemoryStream (buffer);
var writer = new BinaryWriter (stream, Encoding.Unicode);
Serialize (writer);
writer.Dispose ();
stream.Dispose ();
}
public void Serialize (BinaryWriter writer)
{
writer.Write (A);
writer.Write (B);
var str = Encoding.Unicode.GetBytes (C);
writer.Write (str.Length);
writer.Write (str);
writer.Write (D);
}
public void Deserialize (byte [] buffer)
{
var stream = new MemoryStream (buffer);
var reader = new BinaryReader (stream, Encoding.Unicode);
Deserialize (reader);
reader.Dispose ();
stream.Dispose ();
}
public void Deserialize (BinaryReader reader)
{
A = reader.ReadInt32 ();
B = reader.ReadDouble ();
var strLen = reader.ReadInt32 ();
C = Encoding.Unicode.GetString (reader.ReadBytes (strLen));
D = reader.ReadInt64 ();
}
}
Answer 5
When I tried serialization in .net, there were overlaps with different versions of assemblies and something else, i.e. it wasn’t just a flexible set of bytes.
As a good option, I can suggest doing serialization manually , in bytes or xml:
long V = 42;
// bytes:
byte [] = BitConverter.GetBytes (V);
// xml:
xdoc.AddNode (string.Format ("& lt; param type = '{0}' & gt; {1} & lt; / param & gt;", V, V.GetType ()));
Of course, structure is also needed.
Well, and accordingly, depending on the purpose of the application.