/// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dict"></param>
        /// <returns></returns>
        public static T GetObject <T>(this Dictionary <string, object> dict)
        {
            var type = typeof(T);
            var obj  = Activator.CreateInstance(type);

            foreach (var kv in dict)
            {
                object theVal  = null;
                var    propInf = obj.GetPropertyInfo(kv.Key);

                if (propInf.PropertyType.IsGenericType &&
                    propInf.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>) &&
                    !string.IsNullOrEmpty(kv.Value.ToString()))
                {
                    theVal = ReflectionsHelper.ChangeTypeEx(kv.Value, propInf.PropertyType.GetGenericArguments()[0]);
                }
                else
                {
                    if (kv.Value != DBNull.Value)
                    {
                        theVal = ReflectionsHelper.ChangeTypeEx(kv.Value, propInf.PropertyType);
                    }
                }

                // update the value of adlisting sourced from cellentry
                propInf.SetValue(obj, theVal, null);
            }
            return((T)obj);
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileInfo"></param>
        /// <returns></returns>
        public List <T> ImportToList <T>(T obj, MemoryStream memoryStream, string sheet = null) where T : new()
        {
            var list = new List <T>();

            using (var package = new ExcelPackage(memoryStream))
            {
                var worksheet = sheet.IsEmptyString() ? package.Workbook.Worksheets[0] : package.Workbook.Worksheets[sheet];

                var rowCount = worksheet.Dimension?.Rows;
                var colCount = worksheet.Dimension?.Columns;

                if (rowCount.HasValue && colCount.HasValue)
                {
                    for (int row = 2; row <= rowCount.Value; row++)
                    {
                        var currently = new T();
                        for (int col = 1; col <= colCount.Value; col++)
                        {
                            if (worksheet.Cells[1, col].Value != null && worksheet.Cells[row, col].Value != null)
                            {
                                ReflectionsHelper.DynamicBestowValue(currently, worksheet.Cells[1, col].Value.ToString(), worksheet.Cells[row, col].Value.ToString());
                                ReflectionsHelper.DynamicBestowValue(currently, obj);
                            }
                        }
                        list.Add(currently);
                    }
                }
                return(list);
            }
        }
        /// <summary>
        /// Method that extends IQueryable<T> allowing to order query with request properties
        /// </summary>
        /// <typeparam name="TSource">Generic type of the entity</typeparam>
        /// <param name="source">Self IQueryable<T> instance</param>
        /// <param name="request">Self IWrapRequest<T> instance</param>
        /// <returns>Returns IQueryable instance with with the configuration for ordination</returns>
        public static IQueryable <TSource> OrderBy <TSource>(
            this IQueryable <TSource> source,
            IWrapRequest <TSource> request
            ) where TSource : class
        {
            var ordination = request.Ordination();

            string order   = ordination.Order;
            string orderBy = ordination.OrderBy;

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            Type       type   = typeof(TSource);
            var        lambda = LambdasHelper.GenereteLambdaExpression <TSource>(ref type, orderBy);
            MethodInfo orderMethod;

            if (order.ToLower().Equals(Constants.CONST_ORDENATION_ORDER_ASCENDING.ToLower()))
            {
                orderMethod = ReflectionsHelper.GetMethodFromType(typeof(Queryable), "OrderBy", 2, 2);
            }
            else
            {
                orderMethod = ReflectionsHelper.GetMethodFromType(typeof(Queryable), "OrderByDescending", 2, 2);
            }

            return((IQueryable <TSource>)orderMethod.MakeGenericMethod(typeof(TSource), type).Invoke(null, new object[] { source, lambda }));
        }
示例#4
0
        public Func <Task> CreateAction(Func <CancellationToken, Task> action)
        {
            Func <Task> resultAction = async() =>
            {
                var actionId = Interlocked.Increment(ref this.actionCounter);
                try
                {
                    await this.semaphore.WaitAsync();

                    Logger.Trace($"Transition [{actionId}] started");

                    await WaitForFinish();

                    this.cancellationTokenSource = new CancellationTokenSource();
                    this.currentTask             = action(this.cancellationTokenSource.Token)
                                                   .ContinueWith(OnOperationFinished(actionId));
                }
                finally
                {
                    this.semaphore.Release();
                }
            };

#if DEBUG
            resultAction = ReflectionsHelper.WrapActionWithName(resultAction, action.Method.Name);
#endif

            return(resultAction);
        }
示例#5
0
        public Func <Task> CreateAction(Action action)
        {
            Func <Task> resultAction = async() =>
            {
                var actionId = Interlocked.Increment(ref this.actionCounter);
                try
                {
                    await this.semaphore.WaitAsync();

                    Logger.Trace($"Transition [{actionId}] started");
                    await WaitForFinish();

                    // not cancellable, so new cancellation token isn't created
                    action();
                    this.currentTask = Task.CompletedTask;
                    Logger.Trace($"Transition [{actionId}] finished");
                }
                catch (Exception ex)
                {
                    Logger.Error($"Error on handling action [{actionId}]: " + ex);
                }
                finally
                {
                    this.semaphore.Release();
                }
            };

#if DEBUG
            resultAction = ReflectionsHelper.WrapActionWithName(resultAction, action.Method.Name);
#endif

            return(resultAction);
        }
示例#6
0
        public static TModel GetModel <TModel>(this IWrapResponse <TModel> source)
            where TModel : class
        {
            if (source.Data is ICollection <object> )
            {
                throw new Exception("Data is a collection!");
            }

            var model = Activator.CreateInstance <TModel>();

            ReflectionsHelper.Copy(source.Data, model);

            return(model);
        }
        public async Task ReplaceFuncWithExpressions()
        {
            var executed = false;
            var foo      = ReflectionsHelper.WrapActionWithName(() =>
            {
                executed = true;
                this.TestOutput.WriteLine("foo executed");
                return(Task.CompletedTask);
            }, "Foo");

            await foo();

            Assert.Equal("Foo", foo.Method.Name);
            Assert.True(executed);
        }
        public static IList <TModel> GetModels <TModel>(this IWrapResponse <TModel> source)
            where TModel : class
        {
            if (!(source.Data is ICollection <object>))
            {
                throw new Exception("Data is not a collection!");
            }

            var models = new List <TModel>();

            foreach (var data in ((ICollection <object>)source.Data))
            {
                var model = Activator.CreateInstance <TModel>();

                ReflectionsHelper.Copy(data, model);

                models.Add(model);
            }

            return(models);
        }