leejw51 / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
0 stars 0 forks source link

Formatter<T> does not preserve .Context property value in StreamingContext passed to callbacks #130

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
You can contact me at bitserf@gmail.com.

What steps will reproduce the problem?

1. Define a callback with one of the callback attributes
2. Create a new IFormatter using Serializer.CreateFormatter<T>()
3. Create a custom StreamingContext, setting its Context property to a non-null 
object reference.
4. Set the IFormatter.Context property on the returned IFormatter to this 
custom StreamingContext.
5. Perform serialization.

What is the expected output? What do you see instead?

Expected the context passed to callback to include my custom object reference, 
but it is null. 

Since I'm serializing a relatively large tree of objects, doing some work in 
the callback is the best option for me, otherwise I have to traverse the entire 
tree again to do the same thing.

Essentially, what I'm trying to achieve, is register objects that have "keys" 
with an "index", then later on, I can just ask the index to give me back the 
object with key X, for example.

I'd much prefer to have all keyed objects be indexed after deserialization than 
making another pass over the tree :)

What version of the product are you using? On what operating system?

1.0.0.280, Windows 7.

Please provide any additional information below.

Original issue reported on code.google.com by bits...@gmail.com on 7 Aug 2010 at 6:23

GoogleCodeExporter commented 9 years ago
Correction: I am deserializing, not serializing.

Original comment by bits...@gmail.com on 7 Aug 2010 at 6:27

GoogleCodeExporter commented 9 years ago
Hi Mark,

I've attached a diff file (created using Subversion) that fixes this, but it 
was just a quick hack.

Essentially, I made the SerializationCallback delegate accept an additional 
parameter, that being the SerializationContext, since it appeared everywhere 
callbacks were invoked the SerializationContext was available, and that seemed 
like the correct place to piggyback a StreamingContext onto.

However, in the patch you will see these lines:

-                    callback = (SerializationCallback)Delegate.CreateDelegate(
-                        typeof(SerializationCallback), null, found);
+                    callback = delegate(T instance, SerializationContext 
context) { found.Invoke(instance, null); };

Is there a performance improvement when using Delegate.CreateDelegate()? I just 
used the same style of declaration as used by CF2 just to get it to compile, 
otherwise it failed at runtime to bind due to the additional StreamingContext 
parameter.

I apologize for the ugliness of the patch.

Not sure if you want to support this, but if you do, it shouldn't be too much 
effort to do it right :)

Original comment by bits...@gmail.com on 7 Aug 2010 at 8:25

Attachments:

GoogleCodeExporter commented 9 years ago

Original comment by marc.gravell on 6 Sep 2010 at 6:20