Quantcast
Channel: User learning... - Stack Overflow
Viewing all articles
Browse latest Browse all 38

Adding list property using reflection

$
0
0

I am reading a file which is like

HDR …

LIN …

LIN …

LIN …

HDR …

LIN …

LIN …

Now LIN corresponds to line items and belong to the HDR line above it.

Also, mapping for the data comes from xml which has already been deserilized. This has the mappings like which property to store the data in Header or LineItems class, how many chars to pick, if it is required or not etc.

The header model to store data looks like

public class Header{    public string Id { get; set; }    public string Description { get; set; }    public List<Item> LineItems { get; set; }}

I am trying to use a generic function to load data before processing it.

I need help with

  • (#1) in the below function: is it correct way to make sure that property LineItems is not null before adding the LineItem to it?
  • How to do (#2) in below function, using reflection to add lineItem to data[headindex-1].LineItems.Add(lineItem)?

Here is the function...

public static List<H> ReadFile<H, I>(ConfigTypeUploadXml uploadConfig, string fileNamePath, ref string errMsg) where H : new() where I : new(){    // we'll use reflections to add LineItems to data    var properties = typeof(H).GetProperties();    // read the file line by line and add to data.     var data = new List<H>();    var headIndex = 0;    var lineIndex = 1;    foreach (var line in File.ReadAllLines(fileNamePath))    {        // read HDR line        if (line.StartsWith("HDR"))        {            var header = ReadHeaderLine<H>(uploadConfig, line, errMsg);            data.Add(header);            headIndex += 1;        }        // read LIN line        if (line.StartsWith("LIN"))        {            var lineItem = ReadItemLine<I>(uploadConfig, line, errMsg);            foreach (var p in properties)            {                if (p.Name != "LineItems")                    continue;                //1) if items is null then create the object                object items = p.GetValue(data[headIndex - 1], null);                if (items == null)                    p.SetValue(data[headIndex - 1], new List<I>());                //2) add line item to data[headIndex - 1].LineItems.Add(lineItem)            }        }        lineIndex += 1;    }    return data;}

This article Adding items to List<T> using reflection shows how to do it but this in on main object. In my case i need to populate data[index].LineItems.Add(...)


Viewing all articles
Browse latest Browse all 38

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>