Note that there are some explanatory texts on larger screens.

plurals
  1. POSurrogateSelector seems to work only on the root object
    primarykey
    data
    text
    <p>I try to use SurrogatSelector to customize the deserialisation of a stream. It works fine for the root object of the object graph but not for contained objects. See the following code:</p> <pre><code> Stream stream = File.Open("C:\\Temp\\test.bin", FileMode.Create); BinaryFormatter formatter = new BinaryFormatter(); TestToSerialize tts = new TestToSerialize(); formatter.Serialize(stream, tts); stream.Close(); stream = File.Open("C:\\Temp\\test.bin", FileMode.Open); formatter = new BinaryFormatter(); SurrogateSelector ss = new SurrogateSelector(); ss.AddSurrogate(typeof(string), new StreamingContext(StreamingContextStates.All), new StringSerializationSurrogate()); formatter.SurrogateSelector = ss; tts = (TestToSerialize)formatter.Deserialize(stream); stream.Close(); </code></pre> <p>The StringSerializationSurrogate gets called (method SetObjectData) when a string is deserialized, but not when an object containing a string (as a serilizable member) is deserialized. The object to be serialized/ deserialized looks like this:</p> <pre><code> [Serializable] class TestToSerialize { public string s1; public TestToSerialize() { s1 = "some test"; } } </code></pre> <p>Is there a way to have the surrogate get called on non root objects? For completeness the Surrogate looks like this (testcode only to set a breakpoint):</p> <pre><code>sealed class StringSerializationSurrogate : ISerializationSurrogate { public void GetObjectData(Object obj, SerializationInfo info, StreamingContext context) { } public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector) { string s = (String)obj; return obj; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload