0

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:

screenshot

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);
    }
}
galdin
  • 12,411
  • 7
  • 56
  • 71
  • 2
    `TypeID` is not a property but field and it lacks `public` modifier (see "default" `GetProperty` [docs](https://learn.microsoft.com/en-us/dotnet/api/system.type.getproperty?view=net-5.0#System_Type_GetProperty_System_String_)). – Guru Stron Jun 30 '21 at 12:41
  • 1
    solved by adding public modifier and using GetField instead of GetProperty, thank you – Adrian Hernando Solanas Jun 30 '21 at 12:48

0 Answers0