c# - 如何修复 System.Collections.Generic.List?
问题描述
所以基本上我想将列表数据保存到csv。但文件中唯一保存的是:System.Collections.Generic.List`1 [WindowsFormsApp1.Autok],我不知道如何修复,作者搞砸了
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
List<Autok> aLista = new List<Autok>();
Autok a = new Autok();
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
string NewGUID = System.Guid.NewGuid().ToString();
tbTermekkod.Text = NewGUID.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
a.Termekkod = tbTermekkod.Text;
a.Gyarto = tbGyarto.Text;
a.Tipus = tbTipus.Text;
a.Szin = tbSzin.Text;
a.Felszereltsegiszint = tbFelsz.Text;
a.Ar = Convert.ToInt32(tbAr.Text);
aLista.Add(a);
}
private void button3_Click(object sender, EventArgs e)
{
using (StreamWriter sW = new StreamWriter(@"C:\Users\Tamás\source\repos\WindowsFormsApp1\WindowsFormsApp1\bin\Debug\rainbow.csv", false, Encoding.Default))
{
foreach (Autok item in aLista)
{
sW.WriteLine(a);
sW.Close();
}
}
}
}
}
解决方案
正如 Klaus Gütter 所说,您每次都必须创建一个新的 Autok 实例。为了正确保存它,我假设您必须覆盖 Autok 的 ToString 方法。