由于自引用循环,C# 无法序列化我的对象

发布于 2025-01-11 08:44:36 字数 1655 浏览 1 评论 0原文

我正在制作一个图书馆系统和类设计(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

方圜几里 2025-01-18 08:44:36

您可以在序列化时使用ReferenceLoopHandling.Ignore 忽略自引用循环,如果对象是其自身的子对象,则不会序列化该对象。您可以通过将其添加到序列化器设置中来使用它:

JsonConvert.SerializeObject(Author, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = 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:

JsonConvert.SerializeObject(Author, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文