Beispiel #1
0
        public static object?[] GetArgs(ParameterInfo[] ps, object?[] psValue, Func <object?, Task>?callback, CancellationToken token, Stream?stream)
        {
            var psList = ps.ToList();
            var dic    = new Dictionary <int, object?>();

            //Func<>
            var found = psList.FirstOrDefault(i => i.ParameterType.IsFuncT());

            if (found != null)
            {
                dic.Add(psList.IndexOf(found), FuncHelper.ConvertFunc(callback, found.ParameterType.GetGenericArguments()[0]));
            }

            //CancellationToken
            found = psList.FirstOrDefault(i => i.ParameterType.IsCancellationToken());
            if (found != null)
            {
                dic.Add(psList.IndexOf(found), token);
            }

            //Stream
            found = psList.FirstOrDefault(i => i.ParameterType.IsStream());
            if (found != null)
            {
                dic.Add(psList.IndexOf(found), stream);
            }

            var objs        = new List <object?>();
            var psValueList = psValue.ToList();

            //Sort params
            for (var i = 0; i < ps.Length; i++)
            {
                if (dic.Keys.Any(j => j == i))
                {
                    objs.Add(dic[i]);
                }
                else
                {
                    objs.Add(psValueList[0]);
                    psValueList.RemoveAt(0);
                }
            }

            return(objs.ToArray());
        }
        private static (Func <object, Task> callback, CancellationToken token, Stream stream, object[] otherArgs) GetArgs(object[] args)
        {
            var objs = args.ToList();

            //callback
            Func <object, Task> retCallback = null;
            var found = objs.FirstOrDefault(i =>
                                            i != null &&
                                            i.GetType().IsFuncT());

            if (found != null)
            {
                retCallback = FuncHelper.ConvertFunc(found);
                objs.Remove(found);
            }

            //token
            CancellationToken retToken;

            found = objs.FirstOrDefault(i => i is CancellationToken);
            if (found != null)
            {
                retToken = (CancellationToken)found;
                objs.Remove(found);
            }

            //stream
            Stream retStream = null;

            found = objs.FirstOrDefault(i => i is Stream);
            if (found != null)
            {
                retStream = (Stream)found;
                objs.Remove(found);
            }

            //otherArgs
            return(retCallback, retToken, retStream, objs.ToArray());
        }