我有一个看起来像这样的类:
public class Item
{
public Guid Id { get; set; }
public Guid Parent { get; set; }
public string Name { get; set; }
}
如果我序列化它:
var parentId = Guid.NewGuid();
var items = new List<Item>
{
new Item {Id = parentId, Parent = Guid.Empty, Name = "Parent"},
new Item {Id = Guid.NewGuid(), Parent = parentId, Name = "Child"}
};
var builder = new StringBuilder();
var serializer = new YamlDotNet.Serialization.Serializer();
using (var writer = new StringWriter(builder))
{
serializer.Serialize(writer, items);
}
Console.WriteLine(builder.ToString());
输出是:
- Id: 1c49bc54-d0e3-494e-8fd7-b9ffd8c1892a
Name: Parent
- Id: dd54864a-61f7-4d73-b89d-9bd4e4c2c8f4
Parent: *o0
Name: Child
创建了一个引用*o0
,它是另一个类的 ID,它是它的父类。但是 YamlDotnet 没有定义这个引用,因此在反序列化时我得到一个错误,说它缺少一个锚。
这是框架中的错误还是我能做些什么?