How can I set the StreamingContext in Silverlight

2019-07-16 21:48发布

问题:

I need to do a deep copy in Silverlight, which I can do with the tried and tested serialize/deserialize approach. The copied objects aren't exact clones - they need to have some of their properties modified on the copy.

I should be able to do something like this:

[OnDeserialized()]
public void OnDeserializedMethod(StreamingContext context)
{
  if (context.State == StreamingContextStates.Clone)
  {
    //stuff
  }       
}

where the StreamingContext is set up using a NetDataContractSerializer:

NetDataContractSerializer ds = new NetDataContractSerializer(new StreamingContext(StreamingContextStates.Clone));

Silverlight doesn't have a NetDataContractSerializer though :-(.

So is there any way I can set the StreamingContext on the DataContractSerializer to give me something to work with? I can't just blindly apply my changes to every serialize operation, it's only on the specific case of a copy.

Or, alternatively, is there another method that gives me similar hooks into the (de)serialization process so I can play with the data?

(I've looked into implementing IDataContractSurrogate but a) it was painful and b) Silverlight doesn't have one of those either...)

回答1:

I've come to the conclusion that you can't do it, so I guess an alternative approach is in order.