I have a CSV database for equipment (in this case weapons) for a RPG combat tool written in C#/WPF. Currently I am reading the CSV values up through a foreach-loop which splits the complete string line and then proceeds to assign the values one by one through an incrementing index.
This is what my method currently looks like, and it works just fine despite of being ugly.
public static ObservableCollection<Weapon> GetWeaponData()
{
var weaponlist = new ObservableCollection<Weapon>();
var allLines = File.ReadAllLines(WeaponsDbPath);
foreach (var lineValues in allLines.Skip(1).Select(line => line.Split(Separator)))
{
weaponlist.Add(new Weapon()
{
Name = lineValues[0],
EnhancementBonus = Convert.ToInt16(lineValues[1]),
GenerationLevel = Convert.ToInt16(lineValues[2]),
Material = lineValues[3],
//... [around 20 more properties]
});
}
return weaponlist;
}
The thing is, I want to use Reflection to assign the properties based on the header field of the CSV file which looks like this:
Name;EnhancementBonus; GenerationLevel; Material; [...]
Long Sword; 0 ; 0 ; Steel; [...]
Long Bow; 0; 0; Wood; [...]
I further want this deserializer to be flexible and be able to get armor or character data as well.
Anything among the lines of:
foreach string property in csvAllLines[0].Split(';')
{
// find property by name
// set the value found in the split-array
}
Any input would be fantastic :)
Aucun commentaire:
Enregistrer un commentaire