예제 #1
0
        public void DeserializeTypeMismatchTest()
        {
            var testClassPath = GetTempFileName("xml");

            Assert.NotNull(testClassPath);
            var first     = "bob";
            var last      = "smith";
            var testClass = new XTestClassWith2Properties(first, last);;

            Assert.AreEqual(first, testClass.First);
            Assert.AreEqual(last, testClass.Last);
            Assert.IsInstanceOf <XTestClassWith2Properties>(testClass);

            if (File.Exists(testClassPath))
            {
                File.Delete(testClassPath);
            }
            Assert.IsFalse(File.Exists(testClassPath));

            serialize(testClass, testClassPath, null);
            Assert.IsTrue(File.Exists(testClassPath));

            var deserialized = deserialize <XTestClassWith2Properties>(testClassPath, null);

            Assert.IsInstanceOf <XTestClassWith2Properties>(deserialized);
            Assert.AreEqual(testClass, deserialized);

            // Attempt to deserialize with type mis-match
            Assert.Throws <InvalidOperationException>(() => deserialize <XTestClassWithList>(testClassPath, null));

            if (File.Exists(testClassPath))
            {
                File.Delete(testClassPath);
            }
        }
예제 #2
0
        public void SerializeDeserializeClassTest()
        {
            var testClassPath = GetTempFileName("xml");
            var first         = "bob";
            var last          = "smith";
            var testClass     = new XTestClassWith2Properties(first, last);

            Assert.IsInstanceOf <XTestClassWith2Properties>(testClass);

            Assert.AreEqual(first, testClass.First);
            Assert.AreEqual(last, testClass.Last);

            if (File.Exists(testClassPath))
            {
                File.Delete(testClassPath);
            }
            Assert.IsFalse(File.Exists(testClassPath));

            serialize(testClass, testClassPath, null);
            Assert.IsTrue(File.Exists(testClassPath));

            var deserialized = deserialize <XTestClassWith2Properties>(testClassPath, null);

            Assert.IsInstanceOf <XTestClassWith2Properties>(deserialized);
            Assert.AreEqual(testClass, deserialized);
            Assert.IsTrue(testClass.Equals(deserialized));

            if (File.Exists(testClassPath))
            {
                File.Delete(testClassPath);
            }
        }