mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-04-02 06:26:35 +08:00
添加了@Dtc(数据类型转换)、@Data(获取全局数据)表达式
This commit is contained in:
@@ -201,6 +201,10 @@ namespace Serein.Library.Utils
|
||||
result = nuint.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
#endif
|
||||
else if(type == typeof(DateTime))
|
||||
{
|
||||
return DateTime.Parse(valueStr);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("非预期值类型");
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Serein.Library.Api;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
@@ -11,6 +12,39 @@ namespace Serein.Library.Utils
|
||||
public static class SereinEnv
|
||||
{
|
||||
private static IFlowEnvironment environment;
|
||||
|
||||
/// <summary>
|
||||
/// 记录全局数据
|
||||
/// </summary>
|
||||
public static ConcurrentDictionary<string, object> EnvGlobalData { get; } = new ConcurrentDictionary<string, object>();
|
||||
/// <summary>
|
||||
/// 清空全局数据
|
||||
/// </summary>
|
||||
public static void ClearGlobalData()
|
||||
{
|
||||
foreach (var nodeObj in EnvGlobalData.Values)
|
||||
{
|
||||
if (nodeObj != null)
|
||||
{
|
||||
if (typeof(IDisposable).IsAssignableFrom(nodeObj?.GetType()) && nodeObj is IDisposable disposable)
|
||||
{
|
||||
disposable?.Dispose();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
EnvGlobalData.Clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 设置运行流程
|
||||
/// </summary>
|
||||
/// <param name="environment"></param>
|
||||
public static void SetEnv(IFlowEnvironment environment)
|
||||
{
|
||||
if (environment != null)
|
||||
@@ -18,10 +52,22 @@ namespace Serein.Library.Utils
|
||||
SereinEnv.environment = environment;
|
||||
}
|
||||
}
|
||||
public static void WriteLine(InfoType type, string message, InfoClass @class = InfoClass.Trivial)
|
||||
|
||||
/// <summary>
|
||||
/// 输出内容
|
||||
/// </summary>
|
||||
/// <param name="type">类型</param>
|
||||
/// <param name="message">内容</param>
|
||||
/// <param name="class">级别</param>
|
||||
public static void WriteLine(InfoType type, string message, InfoClass @class = InfoClass.General)
|
||||
{
|
||||
SereinEnv.environment.WriteLine(type,message,@class);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
@@ -83,6 +84,16 @@ namespace Serein.Library.Utils.SereinExpression
|
||||
isChange = true;
|
||||
result = SetMember(targetObJ, operand);
|
||||
}
|
||||
else if (operation == "@dtc")
|
||||
{
|
||||
isChange = true;
|
||||
result = DataTypeConversion(targetObJ, operand);
|
||||
}
|
||||
else if (operation == "@data")
|
||||
{
|
||||
isChange = true;
|
||||
result = GetGlobleData(targetObJ, operand);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotSupportedException($"Operation {operation} is not supported.");
|
||||
@@ -402,11 +413,106 @@ namespace Serein.Library.Utils.SereinExpression
|
||||
{
|
||||
return ComputedNumber<decimal>(value, expression);
|
||||
}
|
||||
|
||||
private static T ComputedNumber<T>(object value, string expression) where T : struct, IComparable<T>
|
||||
{
|
||||
T result = value.ToConvert<T>();
|
||||
return SerinArithmeticExpressionEvaluator<T>.Evaluate(expression, result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据类型转换
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="expression"></param>
|
||||
/// <returns></returns>
|
||||
private static object DataTypeConversion(object value, string expression)
|
||||
{
|
||||
Type tempType;
|
||||
|
||||
int typeStartIndex = expression.IndexOf('<');
|
||||
int typeEndIndex = expression.IndexOf('>');
|
||||
string typeStr = expression.Substring(typeStartIndex + 1, typeEndIndex - typeStartIndex - 1)
|
||||
.Trim().ToLower(); // 手动置顶的类型
|
||||
string valueStr = expression.Substring(typeEndIndex + 1, expression.Length - typeEndIndex - 1);
|
||||
switch (typeStr)
|
||||
{
|
||||
case "bool":
|
||||
tempType = typeof(bool);
|
||||
break;
|
||||
case "float":
|
||||
tempType = typeof(float);
|
||||
break;
|
||||
case "decimal":
|
||||
tempType = typeof(decimal);
|
||||
break;
|
||||
case "double":
|
||||
tempType = typeof(double);
|
||||
break;
|
||||
case "sbyte":
|
||||
tempType = typeof(sbyte);
|
||||
break;
|
||||
case "byte":
|
||||
tempType = typeof(byte);
|
||||
break;
|
||||
case "short":
|
||||
tempType = typeof(short);
|
||||
break;
|
||||
case "ushort":
|
||||
tempType = typeof(ushort);
|
||||
break;
|
||||
case "int":
|
||||
tempType = typeof(int);
|
||||
break;
|
||||
case "uint":
|
||||
tempType = typeof(uint);
|
||||
break;
|
||||
case "long":
|
||||
tempType = typeof(long);
|
||||
break;
|
||||
case "ulong":
|
||||
tempType = typeof(ulong);
|
||||
break;
|
||||
// 如果需要支持 nint 和 nuint
|
||||
// case "nint":
|
||||
// tempType = typeof(nint);
|
||||
// break;
|
||||
// case "nuint":
|
||||
// tempType = typeof(nuint);
|
||||
// break;
|
||||
case "string":
|
||||
tempType = typeof(string);
|
||||
break;
|
||||
case "datetime":
|
||||
tempType = typeof(DateTime);
|
||||
break;
|
||||
default:
|
||||
tempType = Type.GetType(typeStr);
|
||||
break;
|
||||
}
|
||||
|
||||
if (tempType.IsValueType)
|
||||
{
|
||||
return valueStr.ToValueData(tempType);
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取全局数据
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="expression"></param>
|
||||
/// <returns></returns>
|
||||
private static object GetGlobleData(object value, string expression)
|
||||
{
|
||||
var keyName = expression;
|
||||
SereinEnv.EnvGlobalData.TryGetValue(keyName, out var data);
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user