由于自引用循环,C# 无法序列化我的对象
我正在制作一个图书馆系统和类设计(https://i.sstatic.net/5HlUf. png) 已提供给我们。 这些是我的课程,并且都具有彼此的列表引用。当我尝试序列化它们时,我遇到了自引用循环异常。我明白问题存在的原因,只是不知道如何解决。
class Author
{
public Author(int AuthorID, string Name, int YearOfBirth, List<Book> Books,string Country)
{
this.AuthorID = AuthorID;
this.Name = Name;
this.YearOfBirth = YearOfBirth;
this.Books = Books;
this.Country = Country;
}
public int AuthorID { get; private set; }
public string Name { get; private set; }
public int YearOfBirth { get; private set; }
//[JsonIgnore]
public List<Book> Books { get; private set; }
public string Country { get; private set; }
}
class Book
{
public Book() { }
public Book(int ISBN, string Title, string Genre, List<Author> Authors, int Year, string Description)
{
this.ISBN = ISBN;
this.Title = Title;
this.Genre = Genre;
this.Authors = Authors;
this.Year = Year;
this.Description = Description;
}
public int ISBN { get; private set; }
public string Title { get; private set; }
public string Genre { get; private set; }
//[JsonIgnore]
public List<Author> Authors { get; private set; }
public int Year { get; private set; }
public string Description { get; private set; }
```
[1]: https://i.sstatic.net/5HlUf.png
I am making a library system, and the class design(https://i.sstatic.net/5HlUf.png) has already been provided to us .
These are my classes, and both have List references to each other. When I try to serialize them, I am faced with a self-referencing loop exception. I understand why the problem exists, I just don't know how to solve it.
class Author
{
public Author(int AuthorID, string Name, int YearOfBirth, List<Book> Books,string Country)
{
this.AuthorID = AuthorID;
this.Name = Name;
this.YearOfBirth = YearOfBirth;
this.Books = Books;
this.Country = Country;
}
public int AuthorID { get; private set; }
public string Name { get; private set; }
public int YearOfBirth { get; private set; }
//[JsonIgnore]
public List<Book> Books { get; private set; }
public string Country { get; private set; }
}
class Book
{
public Book() { }
public Book(int ISBN, string Title, string Genre, List<Author> Authors, int Year, string Description)
{
this.ISBN = ISBN;
this.Title = Title;
this.Genre = Genre;
this.Authors = Authors;
this.Year = Year;
this.Description = Description;
}
public int ISBN { get; private set; }
public string Title { get; private set; }
public string Genre { get; private set; }
//[JsonIgnore]
public List<Author> Authors { get; private set; }
public int Year { get; private set; }
public string Description { get; private set; }
```
[1]: https://i.sstatic.net/5HlUf.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在序列化时使用ReferenceLoopHandling.Ignore 忽略自引用循环,如果对象是其自身的子对象,则不会序列化该对象。您可以通过将其添加到序列化器设置中来使用它:
You can ignore the self-referencing loops while serializing by using
ReferenceLoopHandling.Ignore
which will not serialize an object if it is a child object of itself. You can use it by adding it in your serializer settings: