How to set StreamingContext for DataContractSerializer?

I have code 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);

and I need to provide a structure StreamingContext. I found some links that this can be done for NetDataContractSerializer, but not for DataContractSerializer.

  • Is there any way to make this work?
  • Am I just doing it wrong?
+5
source share
1 answer

Using IDataContractSurrogate allows you to perform custom JSON serialization and deserialization for each type or for each object.

See IDataContractSurrogate methods for API documents and see DataContract Surrogate for a sample.

, JSON, JsonReaderWriterFactory, XML factory. . JsonReaderWriterFactory Class, , .

, !

0

All Articles