如何从文本文件创建 Class 的实例?

How to Create an Instance of a Class from a Text File?

这是一个继承自 IAppointments 的约会 class。 我正在尝试写入一个名为 Appointments.txt 的文本文件,以便我可以将约会存储在文本文件中。我遇到的问题是: “_AppList.Add(Line)”行引发错误 "The best overloaded method match for'System.Collections.Generic.ICollection<Calendar.IAppointment>.Add(Calendar.IAppointment)",这意味着我需要传递一个 IAppointment 而不是字符串行,我不确定我是如何去做的。 其次,我在 foreach 循环中收到一条错误消息,提示“无法将类型 'Calendar.IAppointment' 转换为 'string'。我想要做的就是读取每个字符串,以便我可以创建一个新的实例一个约会并保存那个实例。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using System.ComponentModel;
using System.Drawing;
using System.Collections;
using System.IO;

namespace Calendar
{
    class Appointments : IAppointments
    {
    string line;
    DateTime nStart;
    int nLength;
    string nSubject;
    string nLocation;
    DateTime nDate;

    public Appointments()
    {

    }               

    IList<IAppointment> listApps = new List<IAppointment>();
    IList<IAppointment> _AppList = new List<IAppointment>();

    public bool Load()
    {
        using (var fileReader = new StreamReader(@"D:\All Documents\CalendarApplication_StartingSolution\Appointments.txt"))
        {
            line = fileReader.ReadLine();
            if (line != null)
            {
                string s = line;
                string[] split = s.Split('\t');
                nDate = Convert.ToDateTime(split[0]);
                nStart = Convert.ToDateTime(split[1]);
                nLength = Convert.ToInt32(split[2]);
                nSubject = split[3];
                nLocation = split[4];
                listApps.Add(new Appointment(nSubject, nLocation, nStart, nLength, nDate));
                return true;
            }
        }            
        return false;
    }

    public bool Save()
    {
        string sStart;
        string sLength;
        string sDisplayableDescription;

        StreamWriter fileWriter = new StreamWriter(@"D:\All Downloads\CalendarApplication_StartingSolution\Appointments.txt");

        foreach (IAppointment a in listApps)
        {
            sStart = a.Start.ToString();
            sLength = a.Length.ToString();
            sDisplayableDescription = a.DisplayableDescription.ToString();

            string words = sStart + "\t" + sLength + "\t" + sDisplayableDescription;
            fileWriter.WriteLine(words);
        }

        fileWriter.Close();

        StreamReader fileReader = new StreamReader(@"D:\All Documents\CalendarApplication_StartingSolution\Appointments.txt");
        line = fileReader.ReadLine();

        _AppList.Clear();
        while (line != null)
        {
            _AppList.Add(line);
            line = fileReader.ReadLine();
        }

        fileReader.Close();

        IList<IAppointment> replicaList = new List<IAppointment>();
        replicaList.Clear();

        foreach (string word in _AppList)
        {
            string s = word;
            string[] split = s.Split('\t');
            nDate = Convert.ToDateTime(split[0]);
            nStart = Convert.ToDateTime(split[1]);
            nLength = Convert.ToInt32(split[2]);
            nSubject = split[3];
            nLocation = split[4];
            replicaList.Add(new Appointment(nSubject, nLocation, nStart, nLength, nDate));
        }

        bool copy = false;

        for (int i = 0; i < listApps.Count; i++)
        {
            if (listApps[i].Start != replicaList[i].Start && listApps[i].Length != replicaList[i].Length && listApps[i].DisplayableDescription != replicaList[i].DisplayableDescription);
            {
                copy = false;
                break;
            }

            copy = true;
        }

        return copy;        
    }

    public void Clear()
    {
        listApps.Clear();
    }

    IEnumerable<IAppointment> IAppointments.GetAppointmentsOnDate(DateTime date)
    {
        //_AppList.Clear(); wipes the _AppList before it is repopulated to avoid recurring appointments.
        _AppList.Clear();
        //Using a var within IEnumerable, allows the compiler to determine the type of variable.
        //Eg "var i = 10;", the compiler determines that i is an integer.
        foreach (var appointment in listApps)
        {
            if (appointment.Start.Date == date.Date)
            {
                _AppList.Add(appointment);
            }
        }
        return _AppList;
    }

    public void Add(IAppointment item)
    {
        listApps.Add(item);
    }

    //Indexes the item selected in listApps
    public int IndexOf(IAppointment item)
    {
        return listApps.IndexOf(item);
    }

    public void RemoveAt(int index)
    {
        listApps.RemoveAt(index);
    }

    public void Insert(int index, IAppointment item)
    {
        listApps.Insert(index, item);
    }

    public void CopyTo(IAppointment[] array, int arrayIndex)
    {
        listApps.CopyTo(array, arrayIndex);
    }

    public int Count
    {
        get
        {
            return listApps.Count;
        }
    }

    public bool Contains(IAppointment item)
    {
        return listApps.Contains(item);
    }

    public bool IsReadOnly
    {
        get
        {
            return listApps.IsReadOnly;
        }
    }

    public bool Remove(IAppointment item)
    {
        return listApps.Remove(item);
    }

    public IEnumerator<IAppointment> GetEnumerator()
    {
        return listApps.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        yield return GetEnumerator();
    }

    public IAppointment this[int index]
    {
        get
        {
            return listApps[index];
        }
        set
        {
            listApps[index] = value;
        }
    }
}

通常您的代码中已经有了您的解决方案:

  1. 第一个问题:

    while (line != null) { _AppList.Add(line); line = fileReader.ReadLine(); }

这行不通,因为 line 是一个字符串,此方法不会接受它。 因此它应该是:

while (line != null)
            {
                string s = line;
                string[] split = s.Split('\t');
                nDate = Convert.ToDateTime(split[0]);
                nStart = Convert.ToDateTime(split[1]);
                nLength = Convert.ToInt32(split[2]);
                nSubject = split[3];
                nLocation = split[4];
                listApps.Add(new Appointment(nSubject, nLocation, nStart, nLength, nDate));
                line = fileReader.ReadLine();
            }

(但我不确定这一点,因为我不知道你想阅读的文件是什么样的)

  1. 第二题:

    foreach (string word in _AppList) { string s = word; string[] split = s.Split('\t'); nDate = Convert.ToDateTime(split[0]); nStart = Convert.ToDateTime(split[1]); nLength = Convert.ToInt32(split[2]); nSubject = split[3]; nLocation = split[4]; replicaList.Add(new Appointment(nSubject, nLocation, nStart, nLength, nDate)); }

可以简化为:

foreach(IAppointment date in _AppList)
{
    replicaList.Add(date);
}