公式字段计算初始化-注册方法等

通过注册的方式,使得公式可以内置一些方法,在使用公式的时候可以直接调用这些方法进行使用,比如sum()等,公式的实现以来插件,见之前文章
  /// <summary>
        /// 公式字段计算初始化-注册方法等
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        private Interpreter Initialization()
        {
            //初始化,并不区分大小写
            var interpreter = new Interpreter(InterpreterOptions.DefaultCaseInsensitive);
            Func<double, double, double> min = (x, y) => Math.Min(x, y);
            Func<double, double, double> max = (x, y) => Math.Min(x, y);
            Func<string, int> len = x => x.Length;
            Func<double[], double> sum = Sum;
            Func<double[], double> average = Average;
            Func<string[], string> concatenate = Concatenate;
            Func<string, string, int> find = (x, y) => x.IndexOf(y);
            Func<string, int, int, string> mid = (x, y, z) => { return x.Substring(y, z); };
            Func<string, string, string> substitute = (x, y) => y;
            Func<string, string> lower = (x) => x.ToLower();
            Func<string, string> upper = (x) => x.ToUpper();
            Func<string, string> trim = (x) => x.Trim();
            Func<DateTime> now = () => DateTime.Now;
            Func<DateTime, DateTime, int> datetime_diff = (x, y) => (x - y).Days;
            Func<DateTime, int, DateTime> workday = (x, y) => x.AddDays(y);
            Func<DateTime, DateTime, double> workday_diff = (x, y) => (x - y).TotalDays;
            Func<DateTime, DateTime, bool> is_after = (x, y) => x > y;
            Func<DateTime, DateTime, bool> is_same = (x, y) => x == y;
            Func<DateTime, string> datestr = (x) => x.ToString("YYYY-MM-DD");
            Func<DateTime, string> timestr = (x) => x.ToString("HH:mm:ss");
            Func<bool[], bool> and = And;
            Func<bool[], bool> or = Or;
            interpreter.SetFunction("pow", Math.Pow)
                .SetFunction("min", min)
                .SetFunction("max", max)
                .SetFunction("len", len)
                .SetFunction("sum", sum)
                .SetFunction("average", average)
                .SetFunction("concatenate", concatenate)
                .SetFunction("find", find)
                .SetFunction("mid", mid)
                .SetFunction("substitute", substitute)
                .SetFunction("lower", lower)
                .SetFunction("upper", upper)
                .SetFunction("trim", trim)
                .SetFunction("now", now)
                .SetFunction("datetime_diff", datetime_diff)
                .SetFunction("workday", workday)
                .SetFunction("workday_diff", workday_diff)
                .SetFunction("is_after", is_after)
                .SetFunction("is_same", is_same)
                .SetFunction("datestr", datestr)
                .SetFunction("timestr", timestr)
                .SetFunction("and", and)
                .SetFunction("or", or)
                ;
            return interpreter;
        }

一些需要多个元素的方法无法直接使用内置的委托,需要自定义方法进行使用,下面是一些自定义的委托方法

       #region 公式方法
        /// <summary>
        /// 将所有数值相加。
        /// </summary>
        /// <param name="vs"></param>
        /// <returns></returns>
        public double Sum(params double[] vs)
        {
            double sum = 0;
            foreach (var item in vs)
            {
                sum += item;
            }
            return sum;
        }
        /// <summary>
        /// 返回多个数值的算术平均数。
        /// </summary>
        /// <param name="vs"></param>
        /// <returns></returns>
        public double Average(params double[] vs)
        {
            double sum = 0;
            foreach (var item in vs)
            {
                sum += item;
            }
            return sum / vs.Length;
        }
        /// <summary>
        /// 将多个文本值串联成单个文本值
        /// </summary>
        /// <param name="vs"></param>
        /// <returns></returns>
        public string Concatenate(params string[] vs)
        {
            return string.Join(",", vs);
        }
        /// <summary>
        /// 查找特定的文本在内容中第一次出现的位置
        /// </summary>
        /// <param name="text">指定文本</param>
        /// <param name="findText">要查找的文本</param>
        /// <returns></returns>
        public int Find(string text, string findText)
        {
            return text.IndexOf(findText);
        }
        /// <summary>
        /// 从内容中特定位置提取一段固定长度的文本
        /// </summary>
        /// <param name="text"></param>
        /// <param name="index"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public string Mid(string text, int index, int count)
        {
            return text.Substring(index, count);
        }
        /// <summary>
        /// 如果所有参数均为真( true ),则返回真( true ),否则返回假( false )。
        /// </summary>
        /// <param name="vs"></param>
        /// <returns></returns>
        public bool And(params bool[] vs)
        {
            return !vs.Any(x => false);
        }
        /// <summary>
        /// 如果任何一个参数为真( true ),则返回真( true ),否则返回假( false )
        /// </summary>
        /// <param name="vs"></param>
        /// <returns></returns>
        public bool Or(params bool[] vs)
        {
            return vs.Any(x => true);
        }


        #endregion

在初始化过后,进行解析公式的时候就可以直接使用了

posted @ 2022-05-24 19:58  咳咳Pro  阅读(32)  评论(0)    收藏  举报