You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classTestClass{[BinaryIndex(1)]publicstringAAA;[BinaryIndex(0)]publicintBBB;}//order by index then by name, BBB will be the first field to serializebyte[]buffer=Serializer.Serialize(newTestClass(){AAA="AAA",BBB=0});TestClassstu=Serializer.Deserialize<TestClass>(buffer);
BinaryConverterAttribute
classStudent{//this field with use MyStringConverter to (de)serialize//but Serializer.SerializeWithTypeInfo and Serializer.DeserializeWithTypeInfo will ignore this attribute and use default internal StringConverter[BinaryConverter(typeof(MyStringConverter))]publicstringName;publicintId;}byte[]buffer=Serializer.Serialize(newStudent(){Name="Name001",Id=1000});Studentstu=Serializer.Deserialize<Student>(buffer);//stu.Name based on MyStringConverter's behavior && stu.Id == 1000
classStudent{publicstringName;publicintId;}varstu=newStudent(){Name="Name001",Id=1000};varmemory=newMemoryStream();Serializer.SerializeWithTypeInfo(stu,memory);memory.Seek(0,SeekOrigin.Begin);//result is a ExpaandoObjectdynamicresult=Serializer.DeserializeWithTypeInfo(memory);//result.Name == "Name001" && result.Id == 1000
classStudent{publicstringName;publicintId;//you can add default value[BinaryConstructor("name")]//this ctor can be privatepublicStudent(stringname,intid){Name=name;Id=id;}publicStudent(){//if this default ctor exists//[BinaryConstructor] is meaningless//or it's required}}
//HashSetConverter already exists[GenericConverter(typeof(HashSet<>))]classHashSetConverter:GenericConverter{publicoverrideobjectGenericReadBytes(Streamstream){objecthashset=Activator.CreateInstance(CurrentType);varhashset_Add=CurrentType.GetMethod("Add");intcount=stream.ReadInt32();for(inti=0;i<count;i++){hashset_Add.Invoke(hashset,Serializer.Deserialize(TypeArgs[0],stream));}returnhashset;}publicoverridevoidGenericWriteBytes(Streamstream,objectobj){//ListConverter is internal, here just for exampleListConverter.ListWriteBytes(TypeArgs[0],stream,obj);}}//AddConverterSerializer.AddConverter<HashSetConverter>();
ObjectConverter
//Whether to use it depends on the situation//ObjectConverter does not have basic size check and null check//Use it for external information that only has a certain data structureclassDataInfo{//use BinaryIndex ensure that the data is read in the correct order[BinaryIndex(0)]publicintBBB;[BinaryIndex(1)]publicintAAA;}vardata=ObjectConverter.ReadBytes<DataInfo>(File.OpenRead(@"C:\xxx.bin"));