是否有可能做一个对象的.NET二进制序列化,当你不具备类的源$ C ​​$ C?有可能、当你、做一个、不具备

2023-09-02 10:49:32 作者:一季飘零的思念

我使用的BinaryFormatter 做在C#中的某些对象的二进制序列化。然而,一些对象包含了我通过一个DLL访问,不具备源$ C ​​$ C上课,所以我不能用序列化属性将它们标记。有没有一种简单的方法来他们无论如何序列化?我有一个解决方法这涉及以阶级 NoSource ,使一个新的类 SerializableNoSource 为其构造函数采用 NoSource 对象,并提取所有我需要的资料,但它的哈克。有没有更好的方法?

I am using BinaryFormatter to do binary serialization of some objects in C#. However, some of the objects contain classes that I access via a DLL and do not have the source code for, so I can't mark them with the Serializable attribute. Is there a straightforward way to serialize them anyway? I have a workaround which involves taking class NoSource and making a new class SerializableNoSource for which the constructor takes a NoSource object and extracts all the information I need from it, but it's hacky. Are there any better alternatives?

推荐答案

您可以创建一个系列化替代的。

假设我们有一个引用的程序集,我们有过,没有控制定义的类看起来是这样的:

Imagine that we have a class defined in a referenced assembly that we have no control over that looks like this:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public DriversLicense License;
}


// An instance of this type will be part of the object graph and will need to be 
// serialized also.
public class DriversLicense
{
    public string Number { get; set; }
}

为了序列化这个对象,你需要定义序列化代理为每种类型的对象图。

In order to serialize this object you will need to define a serialization surrogate for each type in the object graph.

要创建序列化代理,你只需要创建一个实现 ISerializationSurrogate 接口:

To create a serialization surrogate you simply need to create a type that implements the ISerializationSurrogate interface:

public class PersonSurrogate : ISerializationSurrogate
{
    /// <summary>
    /// Manually add objects to the <see cref="SerializationInfo"/> store.
    /// </summary>
    public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
    {
        Person person = (Person) obj;
        info.AddValue("Name", person.Name);
        info.AddValue("Age", person.Age);
        info.AddValue("License", person.License);
    }

    /// <summary>
    /// Retrieves objects from the <see cref="SerializationInfo"/> store.
    /// </summary>
    /// <returns></returns>
    public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
    {
        Person person = (Person)obj;
        person.Name = info.GetString("Name");
        person.Age = info.GetInt32("Age");
        person.License = (DriversLicense) info.GetValue("License", typeof(DriversLicense));
        return person;
    }
}

public class DriversLicenseSurrogate : ISerializationSurrogate
{
    /// <summary>
    /// Manually add objects to the <see cref="SerializationInfo"/> store.
    /// </summary>
    public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
    {
        DriversLicense license = (DriversLicense)obj;
        info.AddValue("Number", license.Number);
    }

    /// <summary>
    /// Retrieves objects from the <see cref="SerializationInfo"/> store.
    /// </summary>
    /// <returns></returns>
    public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
    {
        DriversLicense license = (DriversLicense)obj;
        license.Number = info.GetString("Number");
        return license;
    }
}

然后,你需要让你的 IFormatter 了解代理人通过定义和初始化 SurrogateSelector 并将其分配给你的 IFormatter

Then you need to let your IFormatter know about the surrogates by defining and initializing a SurrogateSelector and assigning it to your IFormatter.

private static void SerializePerson(Person person)
{
    if (person == null)
        throw new ArgumentNullException("person");

    using (var memoryStream = new MemoryStream())
    {
        //Configure our surrogate selectors.
        var surrogateSelector = new SurrogateSelector();
        surrogateSelector.AddSurrogate(typeof (Person), new StreamingContext(StreamingContextStates.All),
                                       new PersonSurrogate());
        surrogateSelector.AddSurrogate(typeof (DriversLicense), new StreamingContext(StreamingContextStates.All),
                                       new DriversLicenseSurrogate());

        //Serialize the object
        IFormatter formatter = new BinaryFormatter();
        formatter.SurrogateSelector = surrogateSelector;
        formatter.Serialize(memoryStream, person);

        //Return to the beginning of the stream
        memoryStream.Seek(0, SeekOrigin.Begin);

        //Deserialize the object
        Person deserializedPerson = (Person) formatter.Deserialize(memoryStream);
    }
}

使用序列化代理绝不是简单的,其实可以变得相当冗长,当你试图序列化类型有私人和放大器;需要保护的领域被序列化。

Using a serialization surrogate is by no means straightforward, and can actually become quite verbose when the type you are trying to serialize has private & protected fields that need to be serialized.

但是,正如你已经手动序列化,你所需要的价值观,我不认为这是一个问题。使用替代的是处理一个场景是这样一个更unifom的方式,应该让你感觉更舒服。

But as you are already manually serializing the values you need, I don't think that is an issue. The use of a surrogate is a more unifom way of handling a scenario like this and should make you feel more comfortable.