反序列化时传入对象引用

时间:2011-08-05 14:09:16

标签: c# serialization

我有一个用这个反序列化的对象:

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

2 个答案:

答案 0 :(得分:4)

        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

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

答案 1 :(得分:0)

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

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);