How can I set the StreamingContext in Silverlight DataContractSerizer?

和自甴很熟 提交于 2019-12-24 00:38:02

问题


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.



来源:https://stackoverflow.com/questions/6928380/how-can-i-set-the-streamingcontext-in-silverlight-datacontractserizer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!