コード例 #1
0
        /// <summary>
        ///     Generic extension method yielding objects of specified type from table.
        /// </summary>
        /// <remarks>
        ///     Exceptions are not catched. It works on all or nothing basis.
        ///     Only primitives and enums are supported as property.
        ///     Currently supports only tables with header.
        /// </remarks>
        /// <typeparam name="T">Type to map to. Type should be a class and should have parameterless constructor.</typeparam>
        /// <param name="table">Table object to fetch</param>
        /// <param name="onCaught"></param>
        /// <param name="configurationAction"></param>
        /// <returns>An enumerable of the generating type</returns>
        public static IEnumerable <T> AsEnumerable <T>(this ExcelTable table, OnCaught <T> onCaught = null, Action <IExcelConfiguration <T> > configurationAction = null) where T : class, new()
        {
            IExcelConfiguration <T> configuration = DefaultExcelConfiguration <T> .Instance;

            configurationAction?.Invoke(configuration);

            List <KeyValuePair <int, PropertyInfo> > mapping = PrepareMappings <T>(table);

            ExcelAddress bounds = table.GetDataBounds();

            // Parse table
            for (int row = bounds.Start.Row; row <= bounds.End.Row; row++)
            {
                var item = (T)Activator.CreateInstance(typeof(T));

                foreach (KeyValuePair <int, PropertyInfo> map in mapping)
                {
                    object cell = table.WorkSheet.Cells[row, map.Key + table.Address.Start.Column].Value;

                    PropertyInfo property = map.Value;

                    try
                    {
                        TrySetProperty(item, property, cell);
                    }
                    catch (Exception ex)
                    {
                        if (!configuration.SkipCastingErrors)
                        {
                            var exceptionArgs = new ExcelTableExceptionArgs
                            {
                                ColumnName   = table.Columns[map.Key].Name,
                                ExpectedType = property.PropertyType,
                                PropertyName = property.Name,
                                CellValue    = cell,
                                CellAddress  = new ExcelCellAddress(row, map.Key + table.Address.Start.Column)
                            };

                            throw new ExcelTableConvertException($"The expected type of '{exceptionArgs.PropertyName}' property is '{exceptionArgs.ExpectedType.Name}', but the cell [{exceptionArgs.CellAddress.Address}] contains an invalid value.",
                                                                 ex, exceptionArgs
                                                                 );
                        }
                    }
                }

                onCaught?.Invoke(item, row);

                // TODO:
                if (!configuration.SkipValidationErrors)
                {
                    // Validate parsed object according to data annotations
                    item.Validate(row);
                }

                yield return(item);
            }
        }
コード例 #2
0
 public virtual bool SetPlayer(Player attachedPlayer)
 {
     Curved       = false;
     currentSpeed = baseSpeed;
     player       = attachedPlayer;
     CurrentState = State.CAUGHT;
     OnCaught?.Invoke(this, new FrisbeeEventArgs(player, currentArea));
     return(true);
 }
 /// <summary>
 ///     Returns objects of specified type from the ExcelWorksheet as a list.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="worksheet"></param>
 /// <param name="onCaught"></param>
 /// <param name="configurationAction"></param>
 /// <returns></returns>
 public static List <T> ToList <T>(this ExcelWorksheet worksheet, OnCaught <T> onCaught = null, Action <IExcelConfiguration <T> > configurationAction = null) where T : class, new()
 {
     return(worksheet.AsEnumerable(onCaught, configurationAction).ToList());
 }
        /// <summary>
        ///     Generic extension method yielding objects of specified type from the ExcelWorksheet
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="worksheet"></param>
        /// <param name="onCaught"></param>
        /// <param name="configurationAction"></param>
        /// <returns></returns>
        public static IEnumerable <T> AsEnumerable <T>(this ExcelWorksheet worksheet, OnCaught <T> onCaught = null, Action <IExcelConfiguration <T> > configurationAction = null) where T : class, new()
        {
            IExcelConfiguration <T> configuration = DefaultExcelConfiguration <T> .Instance;

            configurationAction?.Invoke(configuration);

            return(worksheet.AsExcelTable(configuration.HasHeaderRow).AsEnumerable(onCaught, configurationAction));
        }
コード例 #5
0
 public ExcelReadConfiguration <T> Intercept(OnCaught <T> onCaught)
 {
     OnCaught = onCaught;
     return(this);
 }
コード例 #6
0
 /// <summary>
 ///     Converts given ExcelPackage to list of objects
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="package"></param>
 /// <param name="worksheetIndex"></param>
 /// <param name="onCaught"></param>
 /// <param name="configurationAction"></param>
 /// <returns></returns>
 public static List <T> ToList <T>(this ExcelPackage package, int worksheetIndex = 1, OnCaught <T> onCaught = null, Action <IExcelConfiguration <T> > configurationAction = null) where T : class, new()
 {
     return(package.AsEnumerable(worksheetIndex, onCaught, configurationAction).ToList());
 }
コード例 #7
0
 /// <summary>
 ///     Yields objects of specified type from given ExcelPackage
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="package"></param>
 /// <param name="onCaught">Gets created list item and current row index</param>
 /// <param name="configurationAction"></param>
 /// <param name="worksheetIndex"></param>
 /// <returns></returns>
 public static IEnumerable <T> AsEnumerable <T>(this ExcelPackage package, int worksheetIndex = 1, OnCaught <T> onCaught = null, Action <IExcelConfiguration <T> > configurationAction = null) where T : class, new()
 {
     return(package.Workbook.Worksheets[worksheetIndex].AsEnumerable(onCaught, configurationAction));
 }
コード例 #8
0
ファイル: PlayerCaught.cs プロジェクト: impojr/Sombra
 public void Detected()
 {
     AudioManager.Instance.Play(AudioClipName.Caught);
     LevelManager.Instance.timesCaught++;
     OnCaught?.Invoke();
 }
コード例 #9
0
 /// <summary>
 ///     Returns objects of specified type from table as list.
 /// </summary>
 /// <remarks>
 ///     Exceptions are not catched. It works on all or nothing basis.
 ///     Only primitives and enums are supported as property.
 ///     Currently supports only tables with header.
 /// </remarks>
 /// <typeparam name="T">Type to map to. Type should be a class and should have parameterless constructor.</typeparam>
 /// <param name="table">Table object to fetch</param>
 /// <param name="onCaught"></param>
 /// <param name="configurationAction"></param>
 /// <returns>An enumerable of the generating type</returns>
 public static List <T> ToList <T>(this ExcelTable table, OnCaught <T> onCaught = null, Action <IExcelConfiguration <T> > configurationAction = null) where T : class, new()
 {
     return(AsEnumerable(table, onCaught, configurationAction).ToList());
 }