简体   繁体   English

C#二进制序列化

[英]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'. 我正在尝试将对象序列化和反序列化到Byte数组以进行网络通信,我目前有一个接口'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. 我使用BinaryFormater看了一下这个,但我看不出它是否会给我我需要的控件。

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]) 我想能够将一个类装饰为Follows(其中字段可以是任何类型,只要它们是系统类型或者也是[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) 其中所有字段都与网络顺序交换(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 this using the Framework, or do I need to write my own implementation? 所以问题是,我可以使用框架执行此操作,还是需要编写自己的实现?

听起来非常适合BinaryWriterBitConverter

BinarySerializer将完成所有这些以及更多。

I think the project I recently put on github might be just the fight for you. 我认为我最近放在github上的项目可能只是为你而战。 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 退房: https//github.com/Toxantron/CGbR#binary-datacontract-serializer

Input is right now the DataContract and DataMember attribute 现在输入DataContract和DataMember属性

[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;
    }

Take a look at protobuf-net . 看看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. 它是第三方库,但它确实以明确定义的格式存储数据,这些格式可由C#和其他许多语言的其他库读取。

Here is a sample of how to use it: http://code.google.com/p/protobuf-net/wiki/GettingStarted 以下是如何使用它的示例: http//code.google.com/p/protobuf-net/wiki/GettingStarted

I've used it myself and was quite pleased. 我自己用过它并且非常高兴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM