how to access the StreamingContext in Serialization Callbacks

Jan 14, 2010 at 9:40 AM

There is a parameter StreamingContext context in Serialization Callbacks, 

how do I set it before serialization?

Coordinator
Jan 16, 2010 at 6:33 AM
Edited Jan 16, 2010 at 6:33 AM

You don't, Json.NET creates it and uses it to call the callback method. Why do you want to?

Jan 16, 2010 at 1:27 PM

NetDataContractSerializer has a Context property (http://msdn.microsoft.com/en-us/library/system.runtime.serialization.netdatacontractserializer.context.aspx)

so I can use

Serializer.Context = new StreamingContext(StreamingContextStates.Other, myData);

then in the callback, I can do something like

        [OnSerializing]
        internal void OnSerializingMethod(StreamingContext context)
        {
		var myData = context.Context as MyData;

		propertyA = myData.Filename;
		if (myData.IsXXX){
			propertyB = 1;
		} else {
			propertyB = 2;
		}	
	}

 

 

        [OnSerializing]
        internal void OnSerializingMethod(StreamingContext context)
        {