Exemplo n.º 1
0
        public void DerivedClassTest2()
        {
            Car car = new Hatchback {
                Brand = "Toyota", Model = "Corolla", Year = 2008, CanCarryBikes = true
            };
            string generated = SaveToString(new VSContainer {
                Vehicle = car
            });
            string expected = @"<VSContainer xmlns=""clr-namespace:Test.Elements;assembly=XamlTestClasses"">
  <VSContainer.Vehicle>
    <Hatchback>a Toyota 2008 Corolla hatchback which can carry bikes</Hatchback>
  </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());
            }
        }