ISerializationSurrogate中的上下文值为null

时间:2010-11-05 21:48:35

标签: c# serialization

我有一个ISerializationSurrogate实现,我希望将一些数据传递给。根据MSDN文章(http://msdn.microsoft.com/en-us/library/h58hxyt6.aspx),您可以将任意值传递给StreamingContext,然后在ISerializationSurrogate中可用。

public class IndexedSurrogate<T> : ISerializationSurrogate {
    public void GetObjectData(System.Object obj, SerializationInfo info, StreamingContext context) {
        Console.WriteLine(context.Context == null); // <- this is always true
    }
}

我设置了这样的上下文

var surrogateSelector = new SurrogateSelector();
var context = new StreamingContext(StreamingContextStates.All, "my arbitrary data");
surrogateSelector.AddSurrogate(typeof(Transform), context, new IndexedSurrogate<Transform>());

BinaryFormatter formatter = new BinaryFormatter();
formatter.SurrogateSelector = surrogateSelector;
formatter.Serialize(stream, list);

我正在初始化StreamingContext的数据,在本例中,字符串“my arbitrary data”永远不会到达我的IndexedSurrogate类,尽管我的IndexedSurrogate类的GetObjectData方法被调用。我对此完全感到困惑,并且在网上搜索没有任何结果,似乎没有人在StreamingContext上使用附加参数(或者至少没有人在谈论它)。有什么想法吗?

0 个答案:

没有答案
相关问题