请考虑以下代码。 当我尝试通过调用“ SaveToFile”方法进行序列化时,属性“名称”不会被序列化。
有什么想法吗?
public class Subs
{
public string Something { get; set; } = "smew";
}
public class Plep : List<Subs>
{
public string Name { get; set; } = "smew";
public void SaveToFile(string file)
{
using (StreamWriter wrt = new StreamWriter(file))
{
wrt.WriteLine(JsonConvert.SerializeObject(this, Formatting.Indented, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All,
//TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple,
}));
}
}
}
答案 0 :(得分:2)
不要像here那样从List<>
派生您的课程。
将您的班级更改为:
public class Plep
{
public string Name { get; set; } = "smew";
public List<Subs> Subs {get;set;}
public void SaveToFile(string file)
{
using (StreamWriter wrt = new StreamWriter(file))
{
wrt.WriteLine(JsonConvert.SerializeObject(this, Formatting.Indented, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All,
//TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple,
}));
}
}