C# Binary Serialization
I am trying to serialize and deserialize objects to/from a Byte array for network communication, I currently have an interface 'ISerialize'. However I got thinking there should be a more robust way to do this via reflection.
I've looked around a little bit at doing this using BinaryFormater, but I can't see if it will give me the control I require.
EDIT:
I would like to beable to decorate a class as Follows (Where fields can be any type so long as they are a system type or are also [Serializable])
[Serializable]
public class MyClass {
[NonSerialized]
SomeOtherClass _classFeild;
[Position (0)]
UInt16 _field1;
[Position (14)]
UInt32 _feild2;
//..........
}
And have the following functionality,
void Test () {
MyObject = new MyClass ();
Byte[] raw;
raw = Serializer.Serialize (MyClass); // Results in _field1 at raw[0,1]
// _field2 at raw[14-18]
MyClass Deserialized = Serializer.Deserialize<MyClass> (raw);
}
where all fields are swapped to / from network order (bigendian)
I would also rather white list fields to be serialized rather than blacklist those not to be. So the question is, is can I do thi开发者_如何转开发s using the Framework, or do I need to write my own implementation?
Sounds like a good fit for BinaryWriter and BitConverter.
BinarySerializer will do all that and more.
Take a look at protobuf-net. It's a great library for doing exactly what you want. It is a third party library, however it does store the data in a well defined format that can be read by other libraries both in C# and numerous other languages.
Here is a sample of how to use it: http://code.google.com/p/protobuf-net/wiki/GettingStarted
I've used it myself and was quite pleased.
I think the project I recently put on github might be just the fight for you. I claim it will be the fastest binary serializer available and it has zero overhead. Every property is directly represented by its binary value.
Check out: https://github.com/Toxantron/CGbR#binary-datacontract-serializer
Input is right now the DataContract and DataMember attribute
[DataContract]
public partial class Root
{
[DataMember]
public int Number { get; set; }
[DataMember]
public Partial[] Partials { get; set; }
[DataMember]
public IList<ulong> Numbers { get; set; }
}
and the generator output
public byte[] ToBytes(byte[] bytes, ref int index)
{
// Convert Number
Buffer.BlockCopy(BitConverter.GetBytes(Number), 0, bytes, index, 4);;
index += 4;
// Convert Partials
// Two bytes length information for each dimension
Buffer.BlockCopy(BitConverter.GetBytes((ushort)(Partials == null ? 0 : Partials.Length)), 0, bytes, index, 2);
index += 2;
foreach(var value in Partials ?? Enumerable.Empty<Partial>())
{
value.ToBytes(bytes, ref index);
}
// Convert Numbers
// Two bytes length information for each dimension
Buffer.BlockCopy(BitConverter.GetBytes((ushort)(Numbers == null ? 0 : Numbers.Count)), 0, bytes, index, 2);
index += 2;
foreach(var value in Numbers ?? Enumerable.Empty<ulong>())
{
Buffer.BlockCopy(BitConverter.GetBytes(value), 0, bytes, index, 8);;
index += 8;
}
return bytes;
}
精彩评论