示例#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);
        }
示例#2
0
        public TableCell GetCell(string footerName, string headerName)
        {
            int?headerPosition = _headerNamesService.GetHeaderPosition(headerName, _parentTable.ColumnHeaderNames.AsEnumerable <IHeaderInfo>().ToList());

            if (headerPosition == null)
            {
                return(null);
            }

            var node       = _footerService.GetFooterRowCellByName(footerName, (int)headerPosition);
            var footerCell = this.CreateByXpath <TableCell>(node.GetXPath());

            footerCell.Row    = _footerService.Rows.IndexOf(node);
            footerCell.Column = (int)headerPosition;

            return(footerCell);
        }
示例#3
0
        public TableCell GetCell(string headerName)
        {
            int?position = _headerNamesService.GetHeaderPosition(headerName, _parentTable.ColumnHeaderNames.AsEnumerable <IHeaderInfo>().ToList());

            if (position == null)
            {
                return(null);
            }

            return(GetCell((int)position));
        }
示例#4
0
文件: Grid.cs 项目: tamirg7/BELLATRIX
        public IList <GridCell> GetColumn(string header)
        {
            int?position = HeaderNamesService.GetHeaderPosition(header, ControlColumnDataCollection.AsEnumerable <IHeaderInfo>().ToList());

            if (position == null)
            {
                return(new List <GridCell>());
            }

            return(GetColumn((int)position));
        }
示例#5
0
文件: Grid.cs 项目: tamirg7/BELLATRIX
        public GridCell GetCell(string header, int row)
        {
            int?position = HeaderNamesService.GetHeaderPosition(header, ControlColumnDataCollection.AsEnumerable <IHeaderInfo>().ToList());

            if (position == null)
            {
                return(null);
            }

            return(GetCell(row, (int)position));
        }
示例#6
0
        public TableCell GetCell(string header, int row)
        {
            int?position = HeaderNamesService.GetHeaderPosition(header, ColumnHeaderNames.AsEnumerable <IHeaderInfo>().ToList());

            if (position == null)
            {
                return(null);
            }

            return(GetCell((int)position, row));
        }
示例#7
0
文件: Grid.cs 项目: tamirg7/BELLATRIX
        public GridCell GetCell <TDto>(Expression <Func <TDto, object> > expression, int row)
            where TDto : class
        {
            string headerName = HeaderNamesService.GetHeaderNameByExpression <TDto>(expression);
            int?   position   = HeaderNamesService.GetHeaderPosition(headerName, ControlColumnDataCollection);

            if (position == null)
            {
                return(null);
            }

            return(GetCell(row, (int)position));
        }
示例#8
0
        public TableCell GetCell <TDto>(Expression <Func <TDto, object> > expression, int row)
            where TDto : class
        {
            string headerName = HeaderNamesService.GetHeaderNameByExpression <TDto>(expression);
            int?   position   = HeaderNamesService.GetHeaderPosition(headerName, ColumnHeaderNames.AsEnumerable <IHeaderInfo>().ToList());

            if (position == null)
            {
                return(null);
            }

            return(GetCell((int)position, row));
        }
示例#9
0
文件: Grid.cs 项目: tamirg7/BELLATRIX
        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);
        }