Example #1
0
        private static TList MapFieldToProperty <TList, TItem>(DataTable dt, FieldMapperList map)
        {
            try
            {
                var result = (TList)InstanceGenerator.Create(typeof(TList));

                foreach (DataRow dr in dt.Rows)
                {
                    var innerItem = (TItem)InstanceGenerator.Create(typeof(TItem));

                    #region Invoke property setter calls
                    foreach (var item in map)
                    {
                        try
                        {
                            object value = dr[item.ColumnName];
                            if (Convert.IsDBNull(value))
                            {
                                value = null;
                            }
                            DynamicCode.SetProperty(innerItem, item.PropertyName, value);
                        }
                        catch (Exception ex)
                        {
                            if (item.IsRequired)
                            {
                                // set breakpoint here to find out why something fails
                                Debug.WriteLine(ex.Message);
                                Debug.WriteLine(DebuggerTool.Dump(ex));
                                throw;
                            }
                        }
                    }
                    #endregion

                    #region Invoke the Add() method to add the inner item to list
                    try
                    {
                        DynamicCode.InvokeMethod(result, "Add", innerItem);
                    }
                    catch (Exception ex)
                    {
                        // set breakpoint here to find out why something fails
                        Debug.WriteLine(ex.Message);
                        throw;
                    }
                    #endregion
                }

                return(result);
            }
            catch (Exception ex)
            {
                DebuggerTool.AddData(ex, "dt(XML)", DataUtil.DataTableToXml(dt));
                Debug.WriteLine(ex.ToString());
                throw;
            }
        }