C# 将二进制文件读取到列表<;T>;
我正在尝试创建保存到文件中的引号列表。一旦在控制台上显示引号,我将把bool改为true。索引用于处理要在控制台上显示的报价。首先,我尝试了File.writeAllines,但这对我的Quotes类不起作用 似乎我将列表序列化到文件的尝试可以正常工作,但我不知道如何在应该从文件读取到myList2的代码中去掉CS1061 我真的很想得到一些反馈。代码只是为了我自己的学习和娱乐C# 将二进制文件读取到列表<;T>;,c#,list,serialization,deserialization,C#,List,Serialization,Deserialization,我正在尝试创建保存到文件中的引号列表。一旦在控制台上显示引号,我将把bool改为true。索引用于处理要在控制台上显示的报价。首先,我尝试了File.writeAllines,但这对我的Quotes类不起作用 似乎我将列表序列化到文件的尝试可以正常工作,但我不知道如何在应该从文件读取到myList2的代码中去掉CS1061 我真的很想得到一些反馈。代码只是为了我自己的学习和娱乐 using System; using System.Collections.Generic; using Syste
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)
{
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Runtime.Serialization.Formatters.Binary;
名称空间引号
{
//一个引号,后跟一个bool,用于显示最近是否显示过,以及一个用于导航列表的索引。
[可序列化]
类引用
{
私有字符串引用;
所示为私人住宅;
私有整数索引;
公共引号(字符串引号,显示布尔值,整数索引)
{
quote=_quote;
所示=_所示;
指数=_指数;
}
公共字符串引用
{
得到
{
返回报价;
}
设置
{
报价=价值;
}
}
公开展览
{
得到
{
返回显示;
}
设置
{
显示=值;
}
}
公共整数索引
{
得到
{
收益指数;
}
设置
{
指数=价值;
}
}
公共重写字符串ToString()
{
返回string.Format(“{0}{1}{2}”,引号,显示,索引);
}
}
班级计划
{
静态void Main(字符串[]参数)
{
//将变量设置为“我的文档”路径。
字符串mydocpath=Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
//List myList=新列表();
var myList=新列表();
添加(新引号(“一”,假,1));
添加(新引号(“2”,false,2));
添加(新引号(“三”,假,3));
添加(新引号(“四”,假,4));
//将列表写入文件。展开以接受用户输入,并在文件末尾添加。
尝试
{
使用(Stream=File.Open(mydocpath+@“\WriteLines.txt”,FileMode.Create))
{
BinaryFormatter bin=新的BinaryFormatter();
序列化(流,myList);
}
}
捕获(IOException)
{
}
//从文件中读取并写入列表。在有效时输入一个方法。
尝试
{
使用(Stream=File.Open(mydocpath+@“\WriteLines.txt”,FileMode.Open))
{
BinaryFormatter bin=新的BinaryFormatter();
var myList2=(列表)bin.Deserialize(流);
foreach(myList2中的var报价)
{
//为什么这不起作用?我应该在哪里定义报价??
WriteLine(“{0},{1},{2}”,myList2.quote,myList2.show,myList2.index);
}
}
}
捕获(IOException)
{
}
}
}
}
当前,您的代码尝试访问myList2.quote
,但即使在foreach
块中,myList2
仍然是列表本身,而不是“该列表中的当前项”
foreach
将列表中的每个Quote
对象分配给var Quote
变量。在foreach
块中,您可以使用以下方法访问该报价的属性:
Console.WriteLine("{0}, {1}, {2}", quote.Quote, quote.Shown, quote.Index);
(请注意,
quote.quote
是一个私有字段,而quote.quote
是您可以访问的公共属性)当前您的代码尝试访问myList2.quote
,但即使在foreach
块中,myList2
仍然是列表本身,而不是“该列表中的当前项”
foreach
将列表中的每个Quote
对象分配给var Quote
变量。在foreach
块中,您可以使用以下方法访问该报价的属性:
Console.WriteLine("{0}, {1}, {2}", quote.Quote, quote.Shown, quote.Index);
(请注意,
quote.quote
是一个私有字段,而quote.quote
是您可以访问的公共属性)您的foreach循环每次运行时都会在名为quote的变量中创建列表中每个quote的实例
foreach (var quote in myList2)
因此,您应该在循环中的代码中引用该变量
{
Console.WriteLine("{0}, {1}, {2}", quote.Quote, quote.Shown, quote.Index);
}
foreach循环每次运行时都会在名为quote的变量中创建列表中每个quote的实例
foreach (var quote in myList2)
因此,您应该在循环中的代码中引用该变量
{
Console.WriteLine("{0}, {1}, {2}", quote.Quote, quote.Shown, quote.Index);
}
非常感谢。我设法弄糊涂了。现在很明显我犯了错误
也很明显,我需要在哪里工作,如何在这个网站上发布。谢谢你对像我这样的人温柔。非常感谢。我设法弄糊涂了。现在很明显我犯了错误
也很明显,我需要在哪里工作,如何在这个网站上发布。感谢您对我这样的noob表现得温和。要查看您的代码,这里是第二个问题的正确位置:第一条反馈:格式化您的代码。第二条反馈:了解自动实现的属性。第三条反馈:提供一条比这条更短、更准确的错误信息。可能相关:@PriyankKapadia:听起来好像不是真正的代码审查(这也是我的第一个想法),因为OP正在试图摆脱它