开发者

Context value is null in ISerializationSurrogate

开发者 https://www.devze.com 2023-01-23 13:15 出处:网络
I have an ISerializationSurrogate implementation that I want to pass some data in to.According to the MSDN article (http://msdn.microsoft.com/en-us/library/h58hxyt6.aspx) you can pass in an arbitrary

I have an ISerializationSurrogate implementation that I want to pass some data in to. According to the MSDN article (http://msdn.microsoft.com/en-us/library/h58hxyt6.aspx) you can pass in an arbitrary value to your StreamingContext that will then be available in the 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
    }
}

I set up the context like this

var surrogateSelector = new SurrogateSelector();
var context = new StreamingContext(StreamingContextStates.All, "my arbitrary data");
surrogateSelector.AddSurrogate(typeof(T开发者_Python百科ransform), context, new IndexedSurrogate<Transform>());

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

The data I'm initializing the StreamingContext with, in this example the string "my arbitrary data" never arrives in my IndexedSurrogate class, although the GetObjectData method of my IndexedSurrogate class is called. I'm totally stumped on this, and searching the web has turned up nothing, it appears no one is using the additional parameter on the StreamingContext (or at least no one is talking about it). Any ideas?

0

精彩评论

暂无评论...
验证码 换一张
取 消