vendredi 26 mars 2021

Why is this foreach loop missing a property from the class?

I'm basically trying to use reflection to flatten any class into a dictionary so that I can generically use and bind them in Blazor. I then need to be able to create an instance of the class and populate it with the data from the dictionary (which will have been updated by a component).

e.g

public class Order
{
    public Guid Id { get; set; }
    public Customer Customer { get; set; }
    public string Address { get; set; }
    public string Postcode { get; set; }
    public List<string> Test { get; set; }
    public List<Test> Test2 { get; set; }
}

public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName => $"{FirstName} {LastName}";
    public Gender Gender { get; set; }
    public List<string> Test { get; set; }
}

Should become:

{
  "Id": "",
  "Customer.FirstName": "",
  "Customer.LastName": "",
  "Customer.Gender": "",
  "Customer.Test": "",
  "Address": "",
  "Postcode": "",
  "Test": "",
  "Test2": ""
}

For some reason when I iterate the properties of the Order class, Test2 is missed. The loop shows the property in the collection when I put a breakpoint, it just seems to skip it. I've never seen this happen before.

Code: https://dotnetfiddle.net/g1qyVQ

I also don't think the current code with handle further nested depth which I would like it to be able to work with any POCO object really.

Also if anyone knows a better way to do what I'm trying, I would love to find an easier way. Thanks





Aucun commentaire:

Enregistrer un commentaire