Ejemplo n.º 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);
            }
        }
Ejemplo n.º 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);
 }
Ejemplo n.º 3
0
 public void Detected()
 {
     AudioManager.Instance.Play(AudioClipName.Caught);
     LevelManager.Instance.timesCaught++;
     OnCaught?.Invoke();
 }