如何设置的StreamingContext为DataContractSerializer的? [英] How to set the StreamingContext for DataContractSerializer?
问题描述
我有一些code是这样的:
I have some code something like this:
[DataContract]
class Foo {
[OnSerializing]
private void BeforeSerialize(StreamingContext ctx)
{
((MtType)ctx.Context).DoStuff()
}
...
}
var reader = new XmlTextReader(filename);
var serializer = new DataContractSerializer(typeof(Type));
Type type = (Type)serializer.ReadObject(reader);
和我需要提供的StreamingContext
结构。我发现<一href="http://social.msdn.microsoft.com/Forums/en-US/netfxremoting/thread/1db51319-9b1d-4ef1-8cff-1e7813d8a936/">several 的引用,这可以为 NetDataContractSerializer
进行,但没有对的DataContractSerializer
。
and I need to provide the StreamingContext
structure. I have found several references that this can be done for NetDataContractSerializer
but none for DataContractSerializer
.
- 有没有一种方法,使这项工作?
- 我是不是这样做不对?
推荐答案
使用IDataContractSurrogate的让你做的自定义JSON序列化和反序列化每个类型或每个对象的基础上。
The use of IDataContractSurrogate lets you do custom JSON serialization and deserialization on a per-type or per-object basis.
请参阅<一href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.idatacontractsurrogate_methods.aspx"相对=nofollow> IDataContractSurrogate方法,在API文档,看的 DataContract替代为样本。
See IDataContractSurrogate Methods for the API docs and see DataContract Surrogate for the sample.
此外,阅读,跨preT JSON,你将开始与JsonReaderWriterFactory,不是XML工厂。请参阅<一href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.jsonreaderwriterfactory.aspx"相对=nofollow> JsonReaderWriterFactory类以明白我的意思。
Also, to read and interpret JSON, you would start with JsonReaderWriterFactory, not an XML factory. See JsonReaderWriterFactory Class to see what I mean.
希望这有助于!
这篇关于如何设置的StreamingContext为DataContractSerializer的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!