反序列化时传入对象引用

本文关键字:对象引用 反序列化 | 更新日期: 2023-09-27 18:04:50

我有一个对象被反序列化为:

public object DeSerializeObject(string filename)
        {
            object objectToSerialize;
            Stream stream = File.Open(filename, FileMode.Open);
            BinaryFormatter bFormatter = new BinaryFormatter();
            objectToSerialize = (object)bFormatter.Deserialize(stream);
            stream.Close();
            return objectToSerialize;
        }

    public GUI_Settings(SerializationInfo info, StreamingContext ctxt)
    {
        PrinterFont = (Font)info.GetValue("_printFont", typeof(Font));
    }

如何在反序列化时传入和对象?有点像这个

public object DeSerializeObject(string filename, someObject thing)
            {
                ......
                objectToSerialize = (object)bFormatter.Deserialize(stream, thing);
                .....
            }

public GUI_Settings(SerializationInfo info, StreamingContext ctxt, someObject thing)
        {
            PrinterFont = (Font)info.GetValue("_printFont", typeof(Font));
            _thing = thing;
        }

我不太确定我是否解释清楚了,让我再试一次。

在我的主要班级里,我说

_settings = (GUI_Settings)new ObjectSerializing()
    .DeSerializeObject("Settings.data");

或者如果我理解Jalal Alden Saa所说的

_settings = (GUI_Settings)ObjectSerializing
    .DeserializeFromFile<GUI_Settings>("Settings.data");

当代码运行时,我需要向它发送对_backend类型backEndInitializer的引用。此引用包含在这个主类中。GUI_Settings是我正在反序列化的类型_backend是我需要发送GUI_Settings((方法使用的引用。


答案:

public static object DeSerializeObject(string filename, backEndInitializer backend)
    {
    object state = backend; // your object
    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bFormatter =
        new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(
            null,
            new System.Runtime.Serialization.StreamingContext(
                System.Runtime.Serialization.StreamingContextStates.All,
                state)); // pass it in
    object objectToSerialize;
    Stream stream = File.Open(filename, FileMode.Open);
    //BinaryFormatter bFormatter = new BinaryFormatter();
    objectToSerialize = (object)bFormatter.Deserialize(stream);
    stream.Close();
    return objectToSerialize;
}

然后在正在反序列化的类中

public GUI_Settings(SerializationInfo info, StreamingContext ctxt)
        {
            _backend = (backEndInitializer) ctxt.Context;
}

反序列化时传入对象引用

        object state = null; // your object
        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = 
            new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(
                null, 
                new System.Runtime.Serialization.StreamingContext(
                    System.Runtime.Serialization.StreamingContextStates.All, 
                    state)); // pass it in

从那里,您可以在反序列化构造函数的上下文参数中访问it对象。StreamingContext将引用此实例

使用泛型传递将被反序列化的对象的类型:

public static T DeserializeFromFile<T>(string fileName) where T : class
{
    using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
    {
        BinaryFormatter formatter = new BinaryFormatter();
        return (T)formatter.Deserialize(stream);
    }
}

用法:

Thing deserialized = DeserializeFromFile<Thing>(filePath);