using Serein.Library; using Serein.Library.Api; using Serein.Library.Utils; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace Serein.Library { /// /// 脚本代码中常用的函数 /// public static class ScriptBaseFunc { /// /// 获取当前时间 /// /// public static DateTime now() => DateTime.Now; #region 常用的类型转换 /// /// 将值转换为bool类型 /// /// /// public static bool @bool(object value) { return ObjectConvertHelper.ValueParse(value); } /// /// 将值转换为字节类型 /// /// /// public static byte @byte(object value) { return ObjectConvertHelper.ValueParse(value); } /// /// 将值转换为短整型 /// /// /// public static decimal @decimal(object value) { return ObjectConvertHelper.ValueParse(value); } /// /// 将值转换为浮点型 /// /// /// public static float @float(object value) { return ObjectConvertHelper.ValueParse(value); } /// /// 将值转换为双精度浮点型 /// /// /// public static double @double(object value) { return ObjectConvertHelper.ValueParse(value); } /// /// 将值转换为整数类型 /// /// /// public static int @int(object value) { return ObjectConvertHelper.ValueParse(value); } /// /// 将值转换为长整型 /// /// /// public static int @long(object value) { return ObjectConvertHelper.ValueParse(value); } #endregion /// /// 获取集合或数组的长度 /// /// /// /// public static int len(object target) { // 获取数组或集合对象 // 访问数组或集合中的指定索引 if (target is Array array) { return array.Length; } else if (target is string chars) { return chars.Length; } else if (target is IDictionary dict) { return dict.Count; } else if (target is IList list) { return list.Count; } else { throw new ArgumentException($"并非有效集合"); } } /// /// 将对象转换为字符串 /// /// /// public static string str(object obj) { return obj?.ToString() ?? string.Empty; } #region JSON挂载方法 /// /// 转为JSON对象 /// /// /// public static IJsonToken jsonObj(string content) { /*if (string.IsNullOrWhiteSpace(content)) { return JsonHelper.Object(dict => { }) ; }*/ return JsonHelper.Parse(content); } /// /// 转为JSON字符串 /// /// /// public static string jsonStr(object data) { if (data is null) { return "{}"; } return JsonHelper.Serialize(data); } #endregion /// /// 获取全局数据 /// /// /// public static object global(string name) { return SereinEnv.GetFlowGlobalData(name); } /// /// 获取对象的类型 /// /// /// public static Type type(object type) { return type.GetType(); } /// /// 输出内容 /// /// public static void log(object value) { SereinEnv.WriteLine(InfoType.INFO, value?.ToString()); } /// /// 等待一段时间 /// /// /// public static async Task sleep(int value) { await Task.Delay(value); } } }