如何在C#中使用字节数组作为成员序列化和反序列化类 [英] How to serialize and deserialize a class with byte array as a member in c#

查看:82
本文介绍了如何在C#中使用字节数组作为成员序列化和反序列化类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以字节流的形式在2个进程之间发送数据,这对于几乎所有类都适用,但是我遇到的一个问题是,如果对象的类内部有字节数组,则反序列化失败它给我一个错误,指出不能加载发生毛刺的组件。由于发送方和接收方都是不同的应用程序,因此我无法在此处包括该程序集。

I am trying to send data between 2 process in the form of byte streams, which is working fine for almost all the classes but one problem i am having is that the deserilization fails if the class of the object has a byte array inside it and gives me an error stating the assembly where the serilization took place cannot be loaded. I cannot include the assembly in here because both sender and receiver are different applications.

是否可以解决此问题?

编辑:很抱歉,即使是普通的类也无法在另一端反序列化

编辑:还有一个这2种应用程序中的2种使用.Net 2.0

public static byte[] SerializeToByteArray(this object obj)
{
    if (obj == null)
    {
        return null;
    }
    var bf = new BinaryFormatter();
    using (var ms = new MemoryStream())
    {
        bf.Serialize(ms, obj);
        return ms.ToArray();
    }
}

public static T Deserialize<T>(this byte[] byteArray) where T : class
{
    if (byteArray == null)
    {
        return null;
    }
    using (var memStream = new MemoryStream())
    {
        var binForm = new BinaryFormatter();
        memStream.Write(byteArray, 0, byteArray.Length);
        memStream.Seek(0, SeekOrigin.Begin);s
        var obj = (T)binForm.Deserialize(memStream);
        return obj;
    }
}


推荐答案

I建议您使用 DataContractSerializer (此MSDN链接还提供了有关如何使用属性修饰类的示例)。

I'd propose you to use DataContractSerializer (this MSDN link also have example of how class should be decorated with attributes).

然后,您可以将这样的类(反)序列化为/轻松地从二进制形式:

Then you can (de)serialize such class to/from binary form easily:

public static byte[] SerializeToByteArray<T>(this T obj) where T : class
{
    if (obj == null)
    {
        return null;
    }
    using (var ms = new MemoryStream())
    {
        var serializer = new DataContractSerializer(typeof(T));
        serializer.WriteObject(ms, obj);
        return ms.ToArray();
    }
}

public static T Deserialize<T>(this byte[] byteArray) where T : class
{
    if (byteArray == null)
    {
        return default(T);
    }
    using (var memStream = new MemoryStream(byteArray))
    {
        var serializer = new DataContractSerializer(typeof (T));
        var obj = (T) serializer.ReadObject(memStream);
        return obj;
    }
}




使用 return default(T); 而不是 return null; ,因为T可能不可为空 < br>
https:// docs。 microsoft.com/en-us/dotnet/csharp/language-reference/operators/default

use return default(T); instead of return null; , because T might be non-nullable
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/default

[更新]警告!这仍然不是最佳解决方案,因为您必须在两个应用程序中定义完全相等的类,并将它们作为通用参数传递给这些函数

[Update] Warning! this is still not the best solution, as you'll have to define totally equal classes in both applications and pass them as generic parameter to these functions


由于必须使用.net 2.0,因此您需要使用XMLSerialization。它不像DataContract序列化那样方便,但是应该可以。这是在MSDN上此主题的起始页
如下是用于序列化/反序列化的代码

Since you have to use .net 2.0 then you need to fall back to XMLSerialization. It's not as convenient as DataContract serialization, but it should work. Here is starting page for this topic on MSDN Below is code for serialization/deserialization

public static byte[] SerializeToByteArray<T>(this T obj) where T : class
{
    if (obj == null)
    {
        return null;
    }
    using (var ms = new MemoryStream())
    {
        var serializer = new XmlSerializer(typeof(T));
        serializer.Serialize(ms, obj);
        return ms.ToArray();
    }
}

public static T Deserialize<T>(this byte[] byteArray) where T : class
{
    if (byteArray == null)
    {
        return null;
    }
    using (var memStream = new MemoryStream(byteArray))
    {
        var serializer = new XmlSerializer(typeof(T));
        var obj = (T)serializer.Deserialize(memStream);
        return obj;
    }
}

这篇关于如何在C#中使用字节数组作为成员序列化和反序列化类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆