I have this class Item:
public class Item
{
int TypeID;
string Name;
List<string> Flags;
List<Attribute> Attributes;
}
I'm trying to read a file and parse it into that class. File looks like this:
TypeID = 2425
Name = "an ivory chair"
Flags = {Avoid,Rotate,Destroy,Height}
Attributes = {RotateTarget=2423,DestroyTarget=3136}
TypeID = 2426
Name = "a small trunk"
Flags = {Avoid,Destroy,Height}
Attributes = {DestroyTarget=3136}
TypeID = 2427
Name = "a wardrobe"
Flags = {Container,Unpass,Unmove,Unlay}
Attributes = {Capacity=6}
So my idea is to parse it more or less like json serializer (I want to extract it as json after).
When I try to access a member, for example TypeID and assign a value, I'm getting null all time, I can't get a handle to that member so I can access/modify it:
Here's my code attempt:
var path = "items.srv";
// Read file
var lines = File.ReadAllLines(path);
// Make list of attributes
foreach (var line in lines)
{
Item item = new Item();
// Parse
if (line.Contains("TypeID") | line.Contains("Name"))
{
string cleaned = line.Replace(" ", "").Replace("\t", "");
var slices = cleaned.Split('=');
var property = item.GetType().GetProperty(slices[0]);
property.SetValue(item, slices[1], null);
}
}