c#序列化时重命名派生属性
我有一个基类,以列表作为属性。 我有多个派生自该类的类,我希望在序列化时使用不同名称的属性 如果在派生对象中未指定属性,则Json.Net装饰不起作用 如果我指定它,我会得到建议“如果打算隐藏,请使用新关键字”。如果我这样做,我可能会丢失属性上的基本过程c#序列化时重命名派生属性,c#,json,json.net,C#,Json,Json.net,我有一个基类,以列表作为属性。 我有多个派生自该类的类,我希望在序列化时使用不同名称的属性 如果在派生对象中未指定属性,则Json.Net装饰不起作用 如果我指定它,我会得到建议“如果打算隐藏,请使用新关键字”。如果我这样做,我可能会丢失属性上的基本过程 public class Translation { [JsonProperty(PropertyName = "name")] public string Name { get; set; }; [JsonPrope
public class Translation
{
[JsonProperty(PropertyName = "name")]
public string Name { get; set; };
[JsonProperty(PropertyName = "translatedCaption")]
public string TranslatedCaption { get; set; };
public List<Translation> Children { get; set; };
public Translation(string name,string translatedCaption)
{
Name=name;
TranslatedCaption=translatedCaption;
Children = new List<Translation>();
}
public void Add(Translation translation)
{
...
}
}
public class Translation_Model : Translation
{
[JsonProperty(PropertyName = "tables")]
public List<Translation> Children { get; set; };
}
公共类翻译
{
[JsonProperty(PropertyName=“name”)]
公共字符串名称{get;set;};
[JsonProperty(PropertyName=“translatedCaption”)]
公共字符串TranslatedCaption{get;set;};
公共列表子项{get;set;};
公共翻译(字符串名称、字符串翻译标题)
{
名称=名称;
TranslatedCaption=TranslatedCaption;
Children=新列表();
}
公共空白添加(翻译)
{
...
}
}
公共课堂翻译模式:翻译
{
[JsonProperty(PropertyName=“tables”)]
公共列表子项{get;set;};
}
使用不同的json属性名称覆盖派生类中的属性
public class Translation
{
...
public virtual List<Translation> Children { get; set; }
...
}
public class Translation_Model : Translation
{
...
[JsonProperty(PropertyName = "tables")]
public override List<Translation> Children { get; set; }
...
}
公共类翻译
{
...
公共虚拟列表子项{get;set;}
...
}
公共课堂翻译模式:翻译
{
...
[JsonProperty(PropertyName=“tables”)]
公共覆盖列表子项{get;set;}
...
}
virtual
关键字用于修改方法、属性、索引器或
事件声明,并允许在派生
班级