I have some text files that are delimited by commas and I want to read a line, then instantiate it and assign values to the properties. The number of text files will grown in the future but now, I only need to work with a handful of them.
So I created a base class that'll take in a FileInfo
argument, but the problem is how do I assign values to the instance? At the base class, it won't know what the properties names are. I think I should iterate through the properties and assign them by index, but t.GetType().GetProperties()
doesn't return any items.
public class AccountDataFile : DataFileBase<AccountDataFile.Account>
{
public class Account
{
public string Name;
public string Type;
}
public AccountDataFile(FileInfo fiDataFile) : base(fiDataFile) { }
}
base class:
public class DataFileBase<T> where T : new()
{
public List<T> Data;
public DataFileBase(FileInfo fi)
{
this.Data = new List<T>();
var lines = fi.ReadLines();
foreach (var line in lines)
{
var tokens = line.Split(CONSTS.DELIMITER);
var t = new T();
// how to assign values to properties?
this.Data.Add(t);
}
}
}