【问题标题】:Can't add Lazy initialized object to Generic List无法将惰性初始化对象添加到通用列表
【发布时间】:2014-10-25 11:46:38
【问题描述】:

我有通用列表:

 class BooksRegister <T>
    {
        private T[] Register;
        public int Count { get; set; }

       public BooksRegister()
        {
            Register = new T[100];
            Count = 0;
        }

        public void Add(T value)
        {
            if (Count >= 100)
            {
                return;
            }
            Register[Count] = value;
            Count ++;
        }
}

然后是对象类:

class Book
    {
        public String Author { get; set; }
        public String Title { get; set; }
        public int Quantity { get; set; }

        public Book(String aut, String pav, int kiek)
        {
            this.Author = aut;
            this.Title = pav;
            this.Quantity = kiek;
        }

        public override string ToString()
        {
            return Author + " \"" + Title + "\" " + Quantity;
        }
    }

然后是我的Data 课程,我正在从文件中读取信息。我需要实现lazy initialization of object,但是当我这样做时,我无法将我的对象存储在 List 中。

public static void ReadBooks(BooksRegister<Book> allBooks)
        {
            StreamReader sr = new StreamReader("ListOfBooks.txt");

            string line = "";
            while ((line = sr.ReadLine()) != null)
            {
                string[] words = line.Split('|');

                String tempAuthor = words[0];
                String tempTitle = words[1];
                int quant = Convert.ToInt32(words[2]);

                Lazy<Book> tempas = new Lazy<Book>();

                tempas.Value.Author = tempAuthor;
                tempas.Value.Title = tempTitle;
                tempas.Value.Quantity = quant;

                allBooks.Add(tempas); // error here
}

我该如何解决这个问题?我必须使用延迟初始化必要

【问题讨论】:

  • 你试过“allBooks.Add(tempas.Value)”吗?
  • 不,那行得通。但是现在我得到 Missing member exception: The lazily-initialized type does not have a public, parameterless constructor.
  • 当你立即通过最后一行的 tampas.Value 请求新创建对象的值时,有什么懒惰的?
  • @OndrejJanacek 我是惰性对象初始化的新手,但我必须在我的项目中使用此功能。我正在寻找我能做到最好的方式

标签: c# generics lazy-loading lazy-evaluation lazy-initialization


【解决方案1】:

如果你必须使用lazy,有两种方法:

你改变你的惰性初始化代码:

Lazy<Book> tempas = new Lazy<Book>(() => new Book(tempAuthor, tempTitle, quant));
allBooks.Add(tempas.Value);

它的作用是定义如何初始化图书的表达式。这是一种不好的方法,因为您在第一行初始化惰性对象,然后在第二行初始化它,这基本上使得使用 Lazy&lt;Book&gt; 无用。

另一种方法是将方法签名更改为

public static void ReadBooks(BooksRegister<Lazy<Book>> allBooks)

在这种情况下,您的延迟初始化代码如下所示:

Lazy<Book> tempas = new Lazy<Book>(() => new Book(tempAuthor, tempTitle, quant));
allBooks.Add(tempas);

在这种情况下缺少的一件事是如何在BooksRegister 中访问Book,因为现在它是只写对象 - 您可以添加值,但无法从类外部读取它。

【讨论】:

  • 哦,我的错,一切正常。但是您还有其他想法如何使这种惰性初始化更合理地使用吗? @dotnetom
  • @user3662708 延迟初始化很有意义,因为您有一个加载成本很高的对象,而您只想在它使用之前执行它。在您的情况下,如果您将方法 ReadBooks 包装在某种存储库对象中,则使用 Lazy 并仅在需要时初始化和加载所有数据是有意义的。
  • repository object 是什么意思?
猜你喜欢
  • 2020-10-10
  • 1970-01-01
  • 2017-10-24
  • 1970-01-01
  • 2013-02-24
  • 2013-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多