mardi 8 mai 2018

Iterate through any Property, nested objects and Lists C#

I get 2 objects of Type "Shipment" ("Shipment1" and "Shipment2") and must read each value of them. If the value of Shipment1 is NULL/empty I want to look into the same Value of Shipment2 and if the Value is not NULL/empty I have to copy it to Shipment1. I tried to iterate through my objects with Reflection but the nested Objects "Consignor", "Consignee", "Invoices" let me fail. I hope you can help me. I have the following simplified class structure:

public class Shipment
{
    public int Id { get; set; }
    public Address Consignor { get; set; }
    public Address Consignee { get; set; }
    public IEnumerable<Invoice> Invoices{ get; set; }
} 
public class Address
{
    public string Name { get; set; }
    public string Street{ get; set; }
    public string Zipe { get; set; }
    public string City{ get; set; }
    public string Country{ get; set; }
}
public class Invoice
{
    public IEnumerable<Item> Items{ get; set; }
}
public class Item
{
    public string Description{ get; set; }
    public int Amount { get; set; }
}





Aucun commentaire:

Enregistrer un commentaire