有没有办法在不使用C#中的任何库的情况下从CSV文件中单独读取列表和标题?



所以我和我的导师一起制作了一个程序,它是关于创建带有预制对象列表的CSV文件。但由于我想继续进步,我想让它更自动化。

问题是:我可以让我的程序读取CSV文件的头和附加值给它,同时创建新的对象列表没有任何非。net库?

static void Main(string[] args)
{
/*
1. In new files create two classes: House (properties: Id - int, Surface - int, Name - string, IsFlat - bool, Description - string), Human (Id - int, BirthDate - DateTime, Height - byte)
In Houses description use some commas and watch out for them as it is a common csv separator and may cause you some trouble.
2. Create two lists with at least 5 elements each, one with Houses and one with Humans
3. Create two csv files and append to them the right text (check in Excel if it works)
4. Try to extract the data from the files - two new variables that will have exactly the same as the ones created manually in the second step
*/
List<House> houses = new List<House>() {
new House(1, 100, "Stanowski", true, "House has two separate floors, upstairs there is a bedroom and a toilet, downstairs family has living room, kitchen and small garderobe"),
new House(2, 240, "Borek", false, "This house has also two floors, small garden outside and inside there is 3 bedrooms, huge living room and nice - modern kitchen."),
new House(3, 140, "Pol", true, "This house has bald floors"),
new House(4, 280, "Boniek", false, "This house is simply - TOP"),
new House(5, 50, "Lewandowski", true, "This house has golden balls around the living room")
};
List<Human> humans = new List<Human>() {
new Human(1, new DateTime(1980, 7, 21), 185),
new Human(2, new DateTime(1986, 4, 7), 176),
new Human(3, new DateTime(2000, 8, 21), 200),
new Human(4, new DateTime(2004, 2, 20), 145),
new Human(5, new DateTime(1995, 11, 11), 198)
};
var strFilePath = @"C:UsersSzymonsourcereposProjektCSVProjektCSV";
var humanFileName = "humans.csv";
var houseFileName = "houses.csv";
// Take list of properties from an object. - ekspression 'nameof'
var houseColumnNamesString = CsvTools.GetClassPropertiesString<House>();
var humanColumnNamesString = CsvTools.GetClassPropertiesString<Human>();

var humanValues = new StringBuilder("");
foreach (var human in humans)
{
humanValues.Append($"{human.Id};{human.BirthDay};{human.Height};n");
}

using (StreamWriter fileHuman = new StreamWriter(strFilePath + humanFileName, false))
{
fileHuman.WriteLine(humanColumnNamesString);
fileHuman.WriteLine(humanValues);
}
// Separate to different methods/classes 


var readCSV = CsvTools.ReadCsv(strFilePath + houseFileName);
Console.WriteLine(readCSV);


//TODO
// Make automatic reading of properties and it's values, like instead of house.ID in line 72
using (var reader = new StringReader(manyLines))
{
string? item;
do {
item = reader.ReadLine();
Console.WriteLine(item);
} while(item != null);
}
var houseValues = new StringBuilder("");
foreach (var house in houses)
{
houseValues.Append($"{house.Id};{house.Name};{house.Surface};{house.IsFlat};{house.Description};n");
}
using (StreamWriter file = new StreamWriter(strFilePath+houseFileName, false)) // true = add | false = overwrite
{ 
file.WriteLine(houseColumnNamesString);
file.WriteLine(houseValues);
}
} 

好了,这就是我需要的吗?

using(StreamReader file = new StreamReader(strFilePath + houseFileName))
{
string line;
string headers = line.Take(1);
while((line = file.ReadLine()) != null )
{
var content = new List<House>(line);
}    
}

您可以使用c# Reflection遍历集合并构建您的数据。下面的代码片段展示了如何实现这一点。它给了你方法,但我没有完全按照你想要的方式构建字符串:

var houses = new List<House>
{
new House{Id = 1, Name = "Boulevard", Surface = "Plain"},
new House{Id = 2, Name = "Geneva", Surface = "Mat"},
new House{Id = 3, Name = "Lyncroft", Surface = "Carpet"}
};
var properties = Type.GetType("GenericRewriteDateIssueTest.House").GetProperties();
var houseValues = new StringBuilder("");
foreach (var house in houses)
{
foreach (var prop in properties)
{
houseValues.Append($"{prop.GetValue(house)};");
}
}

相关内容

  • 没有找到相关文章

最新更新