public void PropertyTest1() { //A valueSerializer exists on the property and on the type; the ValueSerializer on the property is preferred. Car car = new Sedan { Brand = "Toyota", Model = "Corolla", Trim = "LX", Year = 2008 }; string generated = SaveToString(new VSContainer2 { Vehicle = car }); string expected = @"<VSContainer2 Vehicle=""Car: Toyota"" xmlns=""clr-namespace:Test.Elements;assembly=XamlTestClasses"" />"; Assert.AreEqual(expected, generated); }
public void DerivedClassTest1() { Car car = new Sedan { Brand = "Toyota", Model = "Corolla", Trim = "LX", Year = 2008 }; string generated = SaveToString(new VSContainer { Vehicle = car }); string expected = @"<VSContainer xmlns=""clr-namespace:Test.Elements;assembly=XamlTestClasses""> <VSContainer.Vehicle> <Sedan>a Toyota 2008 Corolla sedan LX</Sedan> </VSContainer.Vehicle> </VSContainer>"; Assert.AreEqual(expected, generated); }
public override string ConvertToString(object value, IValueSerializerContext context) { Car car = (Car)value; StringBuilder sb = new StringBuilder("a " + car.Brand + " " + car.Year + " " + car.Model); if (value is Sedan) { Sedan sedan = (Sedan)value; return(sb.Append(" sedan " + sedan.Trim).ToString()); } else { Hatchback hatchback = (Hatchback)value; string canCarryBikes = hatchback.CanCarryBikes ? "which can carry bikes" : "which cannot carry bikes"; return(sb.Append(" hatchback " + canCarryBikes).ToString()); } }