读二进制到List <t>

时间:2016-07-11 08:29:32

标签: c# list serialization deserialization

我正在尝试创建保存到文件的引号列表。一旦引号显示在控制台上,我就会将bool更改为true。索引用于处理控制台上显示的引用。首先我尝试了File.WriteAllLines,但这不适用于我的Quotes-class。

似乎我尝试将列表序列化为文件将会正常工作,但我无法弄清楚如何在应该从文件读取到myList2的代码中删除CS1061。

我真的希望得到一些反馈。该代码仅供我自己学习和娱乐。

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Quotes
{
    // A quote followed by a bool to show if it has been showed recently and an index to navigate the list.
    [Serializable]
    class Quotes
    {
        private string quote;
        private bool shown;
        private int index;
        public Quotes(string _quote, bool _shown, int _index)
        {
            quote = _quote;
            shown = _shown;
            index = _index;
        }

        public string Quote
        {
            get
            {
                return quote;
            }
            set
            {
                quote = value;
            }
        }

        public bool Shown
        {
            get
            {
                return shown;
            }
            set
            {
                shown = value;
            }
        }

        public int Index
        {
            get
            {
                return index;
            }
            set
            {
                index = value;
            }
        }

        public override string ToString()
        {
            return string.Format("{0} {1} {2}", quote, shown, index);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Set a variable to the My Documents path.
            string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            //List<Quotes> myList = new List<Quotes>();
            var myList = new List<Quotes>();
            myList.Add(new Quotes("One", false, 1));
            myList.Add(new Quotes("Two", false, 2));
            myList.Add(new Quotes("Three", false, 3));
            myList.Add(new Quotes("Four", false, 4));
            //Write the list to a file. Expand to accept user input and add at the end of the file.
            try
            {
                using (Stream stream = File.Open(mydocpath + @"\WriteLines.txt", FileMode.Create))
                {
                    BinaryFormatter bin = new BinaryFormatter();
                    bin.Serialize(stream, myList);
                }
            }
            catch (IOException)
            {
            }

            //Read from a file and write to the list.Put in a method when it works.
            try
            {
                using (Stream stream = File.Open(mydocpath + @"\WriteLines.txt", FileMode.Open))
                {
                    BinaryFormatter bin = new BinaryFormatter();
                    var myList2 = (List<Quotes>)bin.Deserialize(stream);
                    foreach (var quote in myList2)
                    {
                        //Why is this not working? Where should I define quote??
                        Console.WriteLine("{0}, {1}, {2}", myList2.quote, myList2.shown, myList2.index);
                    }
                }
            }
            catch (IOException)
            {
            }
        }
    }
}

3 个答案:

答案 0 :(得分:4)

目前,您的代码尝试访问myList2.quote,但即使在foreach阻止内,myList2仍然是列表本身,而不是“该列表中的当前项目”。

foreach将列表中的每个Quote对象分配给var quote变量。在foreach块中,您可以使用以下命令访问该引用的属性:

Console.WriteLine("{0}, {1}, {2}", quote.Quote, quote.Shown, quote.Index);

(请注意,quote.quote是私有字段,而quote.Quote是您可以访问的公共属性)

答案 1 :(得分:2)

你的foreach循环在每次运行时在列表中创建每个引用的实例,在名为quote

的变量中
foreach (var quote in myList2) 

因此,您应该在循环中的代码中引用该变量。

{
    Console.WriteLine("{0}, {1}, {2}", quote.Quote, quote.Shown, quote.Index);
}

答案 2 :(得分:0)

非常感谢。我设法迷惑自己。现在很明显我在哪里犯了错误。

很明显,我需要研究在这个网站上发布的地点和方式。感谢你和我这样的菜鸟温柔。