Esempio n. 1
0
        private static void LoadRowData(IEnumerable <ColumnInfo> columns, IRow row, object target, IRowInfo rowInfo)
        {
            var errorIndex   = -1;
            var errorMessage = string.Empty;

            foreach (var column in columns)
            {
                var index = column.Attribute.Index;
                if (index < 0)
                {
                    continue;
                }

                try
                {
                    var    cell         = row.GetCell(index);
                    var    propertyType = column.Attribute.PropertyUnderlyingType ?? column.Attribute.Property?.PropertyType;
                    object valueObj;

                    if (!MapHelper.TryGetCellValue(cell, propertyType, out valueObj))
                    {
                        errorIndex   = index;
                        errorMessage = "CellType is not supported yet!";
                        break;
                    }

                    valueObj = column.RefreshAndGetValue(valueObj);

                    if (column.Attribute.TryTake != null)
                    {
                        if (!column.Attribute.TryTake(column, target))
                        {
                            errorIndex   = index;
                            errorMessage = "Returned failure by custom cell resolver!";
                            break;
                        }
                    }
                    else if (propertyType != null && valueObj != null)
                    {
                        // Change types between IConvertible objects, such as double, float, int and etc.
                        var value = MapHelper.ConvertType(valueObj, column);
                        //var value = Convert.ChangeType(valueObj, column.Attribute.PropertyUnderlyingType ?? propertyType);
                        column.Attribute.Property.SetValue(target, value);
                    }
                }
                catch (Exception e)
                {
                    errorIndex   = index;
                    errorMessage = e.Message;
                    break;
                }
            }

            rowInfo.ErrorColumnIndex = errorIndex;
            rowInfo.ErrorMessage     = errorMessage;
        }