public void Test_Polymorhpic_data_contracts() { // normally it isn't necessary to explicitly add subtypes, if those types are defined in *plugins* // but since Common is not a plugin, we need to explicitly add the types PolymorphicDataContractHook <TestPolyDataContractAttribute> .RegisterKnownType(typeof(TestContract4_A)); PolymorphicDataContractHook <TestPolyDataContractAttribute> .RegisterKnownType(typeof(TestContract4_B)); TestContract4 input, output; input = new TestContract4_A() { Data = "foo", DataA = "bar", Child = new TestContract4_B { Data = "x", DataB = "b" } }; var serialized = JsmlSerializer.Serialize(input, "data", new JsmlSerializer.SerializeOptions { Hook = new PolymorphicDataContractHook <TestPolyDataContractAttribute>() }); output = JsmlSerializer.Deserialize <TestContract4>(serialized, new JsmlSerializer.DeserializeOptions { Hook = new PolymorphicDataContractHook <TestPolyDataContractAttribute>() }); Assert.IsInstanceOf(typeof(TestContract4_A), output); Assert.AreEqual("foo", output.Data); Assert.AreEqual("bar", ((TestContract4_A)output).DataA); Assert.IsInstanceOf(typeof(TestContract4_B), output.Child); Assert.AreEqual("x", output.Child.Data); Assert.AreEqual("b", ((TestContract4_B)output.Child).DataB); }
public void InitTest() { PolymorphicDataContractHook <TestPolyDataContractAttribute> .ClearKnownTypes(); }