Beispiel #1
0
        public static IEnumerable <T> BatchDo <T>(this IEnumerable <T> documents,
                                                  Func <List <T>, object> initFunc, Action <List <T>, object> batchFunc, Action endFunc = null, int batchMount = 100)
        {
            var i         = 0;
            var initMount = batchMount / 2;

            if (initMount <= 0)
            {
                initMount = 1;
            }
            bool   isInit = false;
            var    list   = new List <T>();
            object result = null;

            foreach (var document in documents)
            {
                if (document == null)
                {
                    continue;
                }

// list.Add(document.Clone());
                list.Add(document);
                if (!isInit)
                {
                    if (list.Count == initMount)
                    {
                        try
                        {
                            result = initFunc(list);
                        }
                        catch (Exception ex)
                        {
                            XLogSys.Print.Error("batch execute failed " + ex.Message);
                            yield break;
                        }

                        isInit = true;
                    }
                    else
                    {
                        yield return(document);

                        continue;
                    }
                }
                if (list.Count == batchMount)
                {
                    try
                    {
                        batchFunc(list, result);
                    }
                    catch (Exception ex)
                    {
                        XLogSys.Print.Warn(GlobalHelper.Get("key_111") + ex.Message);
                    }

                    list = new List <T>();
                }
                yield return(document);

                i++;
            }
            if (isInit == false)
            {
                try
                {
                    result = initFunc(list);
                }
                catch (Exception ex)
                {
                    XLogSys.Print.Error("batch execute failed " + ex.Message);
                    yield break;
                }
            }
            if (list.Count != 0)
            {
                batchFunc(list, result);
            }
            endFunc?.Invoke();
        }
Beispiel #2
0
        public static object Set(this IDictionary <string, object> dict, string key, object oldValue, Type type = null)
        {
            object value;
            object data = null;

            if (dict == null)
            {
                return(oldValue);
            }
            if (type == null)
            {
                if (oldValue != null)
                {
                    type = oldValue.GetType();
                }
            }
            if (type == null)
            {
                return(null);
            }
            if (dict.TryGetValue(key, out data))
            {
                if (type.IsEnum)
                {
                    if (data is int)
                    {
                        var index = Convert.ChangeType(data, typeof(int));
                        return(index);
                    }
                    else
                    {
                        var item  = data.ToString();
                        var index = Enum.Parse(type, item);
                        return(index);
                    }
                }
                if (type == typeof(XFrmWorkAttribute))
                {
                    var item   = (string)data;
                    var newone = PluginProvider.GetPlugin(item);
                    return(newone);
                }

                if (data == null)
                {
                    return(oldValue);
                }
                try
                {
                    value = Convert.ChangeType(data, type);
                }
                catch (Exception ex)
                {
                    XLogSys.Print.Error(GlobalHelper.Get("key_112") + ex);
                    return(null);
                }

                return(value);
            }

            return(oldValue);
        }