问题描述
我有一些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
结构。我发现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.
请参阅 IDataContractSurrogate方法,在API文档,看的为样本。
See IDataContractSurrogate Methods for the API docs and see DataContract Surrogate for the sample.
此外,阅读,跨preT JSON,你将开始与JsonReaderWriterFactory,不是XML工厂。请参阅 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的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!