Wednesday, 23 January 2013

Object reflection and parameters querying

Note: post is in progress var product = new GasProduct(); // get object property tree to map values on its properties Type productType = product.GetType(); PropertyInfo[] properties = productType.GetProperties(); for (int col = 1; col <= sheetRange.Columns.Count; col++) { try { // Cells names only in the first row of the spreadsheet if (row == 1) { // collect defined names of the collumns var cellName = sheetRange.Cells[row, col].Name.Name; columnNameMap.Add(col, cellName); } else { var cellValue = sheetRange.Cells[row, col].Value2 != null ? sheetRange.Cells[row, col].Value2.ToString() : null; // if "Processed" attribute is not equal "N" (not processed) skip the entire row if (col == 1 && cellValue != "N") break; var prop = (from p in properties where p.Name == columnNameMap[col] select p).FirstOrDefault(); prop.SetValue(product, cellValue, null); } } catch (Exception ex) { string error = "log exception"; } } if (product.IsValid()) { // add product to a bag to fullfill templates lately lately if (_products == null) _products = new List>(); _products.Add(product); } }
Post a Comment