Beispiel #1
0
        internal T CastRow <T>(TableRow row)
            where T : new()
        {
            var props   = typeof(T).GetProperties();
            var castRow = new T();

            foreach (var propertyInfo in props)
            {
                if (row.GetCells().Any())
                {
                    if (propertyInfo.CanWrite)
                    {
                        string headerName     = HeaderNamesService.GetHeaderNameByProperty(propertyInfo);
                        int?   headerPosition = HeaderNamesService.GetHeaderPosition(headerName, ColumnHeaderNames.AsEnumerable <IHeaderInfo>().ToList());

                        // Will skip properties that are not part of the meta data.
                        if (headerPosition != null)
                        {
                            dynamic elementValue = row.GetCells().ToElementList()[(int)headerPosition].InnerText;
                            propertyInfo.SetValue(castRow, elementValue, null);
                        }
                    }
                    else
                    {
                        throw new InvalidOperationException("Cannot cast grid data to C# object. Most probably your C# class properties don't have setters.");
                    }
                }
            }

            return(castRow);
        }
Beispiel #2
0
        public TRowObject CastRow <TRowObject>(int rowIndex, params string[] propertiesToSkip)
            where TRowObject : new()
        {
            var cells = TableService.GetRowCells(rowIndex);

            if (cells.Count != ControlColumnDataCollection.Count)
            {
                // Compare headers to determine why the cells count is different
                var actual   = TableService.Headers.Select(c => c.InnerText.Trim(" 0".ToCharArray())).ToList();
                var expected = ControlColumnDataCollection.Select(c => c.HeaderName).ToList();
                CollectionAssert.AreEqual(expected, actual, $"Expected: {expected.Stringify()}\r\nActual: {actual.Stringify()}");
            }

            var dto        = new TRowObject();
            var properties = dto.GetType().GetProperties().ToList();

            foreach (var propertyInfo in properties)
            {
                string headerName = HeaderNamesService.GetHeaderNameByProperty(propertyInfo);

                if (propertiesToSkip.Contains(headerName))
                {
                    continue;
                }

                int?headerPosition = HeaderNamesService.GetHeaderPosition(headerName, ControlColumnDataCollection.AsEnumerable <IHeaderInfo>().ToList());
                if (headerPosition == null)
                {
                    continue;
                }

                var controlData = GetControlDataByProperty(propertyInfo);
                if (controlData != null && controlData.ElementType != null && controlData.ElementType.IsSubclassOf(typeof(Element)))
                {
                    var     repo      = new ElementRepository();
                    var     xpath     = $".{cells[(int)headerPosition].GetXPath()}";
                    var     tableCell = this.CreateByXpath <TableCell>(xpath);
                    dynamic elementValue;
                    if (controlData.By == null)
                    {
                        controlData.By = new FindXpathStrategy(xpath);
                        elementValue   = CastCell(repo, controlData, tableCell);
                        controlData.By = null;
                    }
                    else
                    {
                        elementValue = CastCell(repo, controlData, tableCell);
                    }

                    var elementType = Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType;

                    var newValue = elementValue == null ? default : Convert.ChangeType(elementValue, elementType);

                                   elementValue = Convert.ChangeType(newValue, propertyInfo.PropertyType);
                                   propertyInfo.SetValue(dto, elementValue);
                }
                else
                {
                    string htmlNodeValue = HttpUtility.HtmlDecode(TableService.GetRowCells(rowIndex)[(int)headerPosition].InnerText).Trim();
                    var    type          = Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType;
                    object elementValue;
                    if (type == typeof(DateTime) || type == typeof(DateTime?))
                    {
                        DateTime dateTime;
                        DateTime.TryParse(htmlNodeValue, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out dateTime);
                        elementValue = (DateTime?)dateTime;
                    }
                    else
                    {
                        elementValue = string.IsNullOrEmpty(htmlNodeValue) ? default : Convert.ChangeType(htmlNodeValue, type, CultureInfo.InvariantCulture);
                    }

                    propertyInfo.SetValue(dto, elementValue);
                }
            }

            return(dto);
        }