来自父类的C#,属性未包含在序列化子类实例中

时间:2016-07-21 02:15:31

标签: c# inheritance derived-class

public class Vehicle
{
    public String Manufacturer;
}

public class Train : Vehicle
{
    public int MaxNumberOfWagons;
}

var aTrain = new Train();
aTrain.Manufacturer = "RailCorpMurica";
aTrain.MaxNumberOfWagons = 30;
var aTrainJson = serialiser.Serialise(aTrain);

//aTrainJson does not have the Manufacturer property?

我正在尝试将aTrain序列化为json。结果包含MaxNumberOfWagons键和值。但制造不包括在内。

如何在不触及Vehicle课程的情况下包含制造商?

1 个答案:

答案 0 :(得分:0)

如何序列化对象?以下代码应该能够得到你想要的东西:

 var aTrainJson = new JavaScriptSerializer().Serialize(aTrain);

确保添加对System.Web.Extension的引用。