mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-03-02 15:50:47 +08:00
1. Workben项目中,优化了Node的入参类型与返回类型包含泛型成员的类型显示。
This commit is contained in:
@@ -342,7 +342,7 @@ namespace Serein.Library
|
||||
}
|
||||
sb.AppendLine();
|
||||
sb.AppendLine($"返回值信息:");
|
||||
sb.AppendLine($"\t{this.ReturnType?.FullName}");
|
||||
sb.AppendLine($"\t{this.ReturnType?.GetFriendlyName()}");
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
|
||||
@@ -499,7 +499,7 @@ namespace Serein.Library
|
||||
/// <returns></returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return $"[{this.Index}] {(string.IsNullOrWhiteSpace(this.Description) ? string.Empty : $"({this.Description})")}{this.Name} : {this.DataType?.FullName}";
|
||||
return $"[{this.Index}] {(string.IsNullOrWhiteSpace(this.Description) ? string.Empty : $"({this.Description})")}{this.Name} : {this.DataType?.GetFriendlyName()}";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace Serein.Library
|
||||
/// <returns></returns>
|
||||
public static bool @bool(object value)
|
||||
{
|
||||
return ConvertHelper.ValueParse<bool>(value);
|
||||
return ObjectConvertHelper.ValueParse<bool>(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -37,7 +37,7 @@ namespace Serein.Library
|
||||
/// <returns></returns>
|
||||
public static byte @byte(object value)
|
||||
{
|
||||
return ConvertHelper.ValueParse<byte>(value);
|
||||
return ObjectConvertHelper.ValueParse<byte>(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -47,7 +47,7 @@ namespace Serein.Library
|
||||
/// <returns></returns>
|
||||
public static decimal @decimal(object value)
|
||||
{
|
||||
return ConvertHelper.ValueParse<decimal>(value);
|
||||
return ObjectConvertHelper.ValueParse<decimal>(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -57,7 +57,7 @@ namespace Serein.Library
|
||||
/// <returns></returns>
|
||||
public static float @float(object value)
|
||||
{
|
||||
return ConvertHelper.ValueParse<float>(value);
|
||||
return ObjectConvertHelper.ValueParse<float>(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -67,7 +67,7 @@ namespace Serein.Library
|
||||
/// <returns></returns>
|
||||
public static double @double(object value)
|
||||
{
|
||||
return ConvertHelper.ValueParse<double>(value);
|
||||
return ObjectConvertHelper.ValueParse<double>(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -77,7 +77,7 @@ namespace Serein.Library
|
||||
/// <returns></returns>
|
||||
public static int @int(object value)
|
||||
{
|
||||
return ConvertHelper.ValueParse<int>(value);
|
||||
return ObjectConvertHelper.ValueParse<int>(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -87,7 +87,7 @@ namespace Serein.Library
|
||||
/// <returns></returns>
|
||||
public static int @long(object value)
|
||||
{
|
||||
return ConvertHelper.ValueParse<int>(value);
|
||||
return ObjectConvertHelper.ValueParse<int>(value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -1,370 +0,0 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Serein.Library.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// 类型转换工具类
|
||||
/// </summary>
|
||||
public static class ConvertHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 字面量转为对应类型
|
||||
/// </summary>
|
||||
/// <param name="valueStr"></param>
|
||||
/// <returns></returns>
|
||||
public static Type ToTypeOfString(this string valueStr)
|
||||
{
|
||||
if (valueStr.IndexOf('.') != -1)
|
||||
{
|
||||
// 通过指定的类型名称获取类型
|
||||
return Type.GetType(valueStr);
|
||||
}
|
||||
|
||||
|
||||
if (valueStr.Equals("bool", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(bool);
|
||||
}
|
||||
#region 整数型
|
||||
else if (valueStr.Equals("sbyte", StringComparison.OrdinalIgnoreCase)
|
||||
|| valueStr.Equals(nameof(SByte), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(SByte);
|
||||
}
|
||||
else if (valueStr.Equals("short", StringComparison.OrdinalIgnoreCase)
|
||||
|| valueStr.Equals(nameof(Int16), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(Int16);
|
||||
}
|
||||
else if (valueStr.Equals("int", StringComparison.OrdinalIgnoreCase)
|
||||
|| valueStr.Equals(nameof(Int32), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(Int32);
|
||||
}
|
||||
else if (valueStr.Equals("long", StringComparison.OrdinalIgnoreCase)
|
||||
|| valueStr.Equals(nameof(Int64), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(Int64);
|
||||
}
|
||||
|
||||
else if (valueStr.Equals("byte", StringComparison.OrdinalIgnoreCase)
|
||||
|| valueStr.Equals(nameof(Byte), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(Byte);
|
||||
}
|
||||
else if (valueStr.Equals("ushort", StringComparison.OrdinalIgnoreCase)
|
||||
|| valueStr.Equals(nameof(UInt16), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(UInt16);
|
||||
}
|
||||
else if (valueStr.Equals("uint", StringComparison.OrdinalIgnoreCase)
|
||||
|| valueStr.Equals(nameof(UInt32), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(UInt32);
|
||||
}
|
||||
else if (valueStr.Equals("ulong", StringComparison.OrdinalIgnoreCase)
|
||||
|| valueStr.Equals(nameof(UInt64), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(UInt64);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 浮点型
|
||||
else if (valueStr.Equals("float", StringComparison.OrdinalIgnoreCase)
|
||||
|| valueStr.Equals(nameof(Single), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(Single);
|
||||
}
|
||||
else if (valueStr.Equals("double", StringComparison.OrdinalIgnoreCase)
|
||||
|| valueStr.Equals(nameof(Double), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(Double);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 小数型
|
||||
|
||||
else if (valueStr.Equals("decimal", StringComparison.OrdinalIgnoreCase)
|
||||
|| valueStr.Equals(nameof(Decimal), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(Decimal);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 其他常见的类型
|
||||
else if (valueStr.Equals(nameof(DateTime), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(DateTime);
|
||||
}
|
||||
|
||||
else if (valueStr.Equals(nameof(String), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(String);
|
||||
}
|
||||
#endregion
|
||||
|
||||
else
|
||||
{
|
||||
throw new ArgumentException($"无法解析的字面量类型[{valueStr}]");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 对象转换为对应类型
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult"></typeparam>
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
public static TResult ToConvert<TResult>(this object data)
|
||||
{
|
||||
var type = typeof(TResult);
|
||||
if (data is null && type.IsValueType)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
return (TResult)data.ToConvertValueType(type);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 对象转换
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public static object ToConvertValueType(this object data, Type type)
|
||||
{
|
||||
if (type.IsValueType)
|
||||
{
|
||||
if (data == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return ConvertHelper.ValueParse(type, data);
|
||||
}
|
||||
}
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 文本
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public static T ValueParse<T>(object value) where T : struct, IComparable<T>
|
||||
{
|
||||
if (value is T data)
|
||||
{
|
||||
return data;
|
||||
}
|
||||
string valueStr = value.ToString();
|
||||
return valueStr.ToValueData<T>() ;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 文本转换数值
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public static object ValueParse(Type type, object value)
|
||||
{
|
||||
string valueStr = value.ToString();
|
||||
return valueStr.ToValueData(type);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 文本转换值对象
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="valueStr"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentException"></exception>
|
||||
public static T ToValueData<T>(this string valueStr) where T : struct, IComparable<T>
|
||||
{
|
||||
if (string.IsNullOrEmpty(valueStr))
|
||||
{
|
||||
throw new NullReferenceException();
|
||||
//return default(T);
|
||||
}
|
||||
var type = typeof(T);
|
||||
object result;
|
||||
if (type.IsEnum)
|
||||
{
|
||||
result = Enum.Parse(type, valueStr);
|
||||
}
|
||||
else if (type == typeof(bool))
|
||||
{
|
||||
result = bool.Parse(valueStr);
|
||||
}
|
||||
else if (type == typeof(float))
|
||||
{
|
||||
result = float.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(decimal))
|
||||
{
|
||||
result = decimal.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(double))
|
||||
{
|
||||
result = double.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(sbyte))
|
||||
{
|
||||
result = sbyte.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(byte))
|
||||
{
|
||||
result = byte.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(short))
|
||||
{
|
||||
result = short.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(ushort))
|
||||
{
|
||||
result = ushort.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(int))
|
||||
{
|
||||
result = int.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(uint))
|
||||
{
|
||||
result = uint.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(long))
|
||||
{
|
||||
result = long.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(ulong))
|
||||
{
|
||||
result = ulong.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
#if NET6_0 || NET7_0 || NET8_0
|
||||
else if (type == typeof(nint))
|
||||
{
|
||||
result = nint.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(nuint))
|
||||
{
|
||||
result = nuint.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
#endif
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("非预期值类型");
|
||||
}
|
||||
|
||||
return (T)result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串转换为指定类型的值对象。
|
||||
/// </summary>
|
||||
/// <param name="valueStr"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentException"></exception>
|
||||
public static object ToValueData(this string valueStr, Type type)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(valueStr))
|
||||
{
|
||||
return Activator.CreateInstance(type);
|
||||
}
|
||||
object result;
|
||||
if (type.IsEnum)
|
||||
{
|
||||
result = Enum.Parse(type, valueStr);
|
||||
}
|
||||
else if (type == typeof(bool))
|
||||
{
|
||||
result = bool.Parse(valueStr);
|
||||
}
|
||||
else if (type == typeof(float))
|
||||
{
|
||||
result = float.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(decimal))
|
||||
{
|
||||
result = decimal.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(double))
|
||||
{
|
||||
result = double.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(sbyte))
|
||||
{
|
||||
result = sbyte.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(byte))
|
||||
{
|
||||
result = byte.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(short))
|
||||
{
|
||||
result = short.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(ushort))
|
||||
{
|
||||
result = ushort.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(int))
|
||||
{
|
||||
result = int.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(uint))
|
||||
{
|
||||
result = uint.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(long))
|
||||
{
|
||||
result = long.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(ulong))
|
||||
{
|
||||
result = ulong.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
#if NET6_0 || NET7_0 || NET8_0
|
||||
else if (type == typeof(nint))
|
||||
{
|
||||
result = nint.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(nuint))
|
||||
{
|
||||
result = nuint.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
#endif
|
||||
else if(type == typeof(DateTime))
|
||||
{
|
||||
if (valueStr.Equals("now"))
|
||||
{
|
||||
return DateTime.Now;
|
||||
}
|
||||
else if (valueStr.Equals("utcnow"))
|
||||
{
|
||||
return DateTime.UtcNow;
|
||||
}
|
||||
return DateTime.Parse(valueStr);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("非预期值类型");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Serein.Library.Utils
|
||||
{
|
||||
@@ -80,5 +82,259 @@ namespace Serein.Library.Utils
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 对象转换为对应类型
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult"></typeparam>
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
public static TResult ToConvert<TResult>(this object data)
|
||||
{
|
||||
var type = typeof(TResult);
|
||||
if (data is null && type.IsValueType)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
return (TResult)data.ToConvertValueType(type);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 对象转换
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public static object ToConvertValueType(this object data, Type type)
|
||||
{
|
||||
if (type.IsValueType)
|
||||
{
|
||||
if (data == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return ObjectConvertHelper.ValueParse(type, data);
|
||||
}
|
||||
}
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 文本
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public static T ValueParse<T>(object value) where T : struct, IComparable<T>
|
||||
{
|
||||
if (value is T data)
|
||||
{
|
||||
return data;
|
||||
}
|
||||
string valueStr = value.ToString();
|
||||
return valueStr.ToValueData<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 文本转换数值
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public static object ValueParse(Type type, object value)
|
||||
{
|
||||
string valueStr = value.ToString();
|
||||
return valueStr.ToValueData(type);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 文本转换值对象
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="valueStr"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentException"></exception>
|
||||
public static T ToValueData<T>(this string valueStr) where T : struct, IComparable<T>
|
||||
{
|
||||
if (string.IsNullOrEmpty(valueStr))
|
||||
{
|
||||
throw new NullReferenceException();
|
||||
//return default(T);
|
||||
}
|
||||
var type = typeof(T);
|
||||
object result;
|
||||
if (type.IsEnum)
|
||||
{
|
||||
result = Enum.Parse(type, valueStr);
|
||||
}
|
||||
else if (type == typeof(bool))
|
||||
{
|
||||
result = bool.Parse(valueStr);
|
||||
}
|
||||
else if (type == typeof(float))
|
||||
{
|
||||
result = float.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(decimal))
|
||||
{
|
||||
result = decimal.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(double))
|
||||
{
|
||||
result = double.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(sbyte))
|
||||
{
|
||||
result = sbyte.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(byte))
|
||||
{
|
||||
result = byte.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(short))
|
||||
{
|
||||
result = short.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(ushort))
|
||||
{
|
||||
result = ushort.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(int))
|
||||
{
|
||||
result = int.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(uint))
|
||||
{
|
||||
result = uint.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(long))
|
||||
{
|
||||
result = long.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(ulong))
|
||||
{
|
||||
result = ulong.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
#if NET6_0 || NET7_0 || NET8_0
|
||||
else if (type == typeof(nint))
|
||||
{
|
||||
result = nint.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(nuint))
|
||||
{
|
||||
result = nuint.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
#endif
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("非预期值类型");
|
||||
}
|
||||
|
||||
return (T)result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串转换为指定类型的值对象。
|
||||
/// </summary>
|
||||
/// <param name="valueStr"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentException"></exception>
|
||||
public static object ToValueData(this string valueStr, Type type)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(valueStr))
|
||||
{
|
||||
return Activator.CreateInstance(type);
|
||||
}
|
||||
object result;
|
||||
if (type.IsEnum)
|
||||
{
|
||||
result = Enum.Parse(type, valueStr);
|
||||
}
|
||||
else if (type == typeof(bool))
|
||||
{
|
||||
result = bool.Parse(valueStr);
|
||||
}
|
||||
else if (type == typeof(float))
|
||||
{
|
||||
result = float.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(decimal))
|
||||
{
|
||||
result = decimal.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(double))
|
||||
{
|
||||
result = double.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(sbyte))
|
||||
{
|
||||
result = sbyte.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(byte))
|
||||
{
|
||||
result = byte.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(short))
|
||||
{
|
||||
result = short.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(ushort))
|
||||
{
|
||||
result = ushort.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(int))
|
||||
{
|
||||
result = int.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(uint))
|
||||
{
|
||||
result = uint.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(long))
|
||||
{
|
||||
result = long.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(ulong))
|
||||
{
|
||||
result = ulong.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
#if NET6_0 || NET7_0 || NET8_0
|
||||
else if (type == typeof(nint))
|
||||
{
|
||||
result = nint.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else if (type == typeof(nuint))
|
||||
{
|
||||
result = nuint.Parse(valueStr, CultureInfo.InvariantCulture);
|
||||
}
|
||||
#endif
|
||||
else if (type == typeof(DateTime))
|
||||
{
|
||||
if (valueStr.Equals("now"))
|
||||
{
|
||||
return DateTime.Now;
|
||||
}
|
||||
else if (valueStr.Equals("utcnow"))
|
||||
{
|
||||
return DateTime.UtcNow;
|
||||
}
|
||||
return DateTime.Parse(valueStr);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("非预期值类型");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
180
Library/Utils/TypeHelper.cs
Normal file
180
Library/Utils/TypeHelper.cs
Normal file
@@ -0,0 +1,180 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
|
||||
namespace Serein.Library.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// 类型转换工具类
|
||||
/// </summary>
|
||||
public static class TypeHelper
|
||||
{
|
||||
|
||||
|
||||
public static string GetFriendlyName(this Type type,bool isFullName = true)
|
||||
{
|
||||
if (type.IsGenericType)
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
string typeName = isFullName? type.FullName : type.Name;
|
||||
int backtickIndex = typeName.IndexOf('`');
|
||||
if (backtickIndex > 0)
|
||||
{
|
||||
typeName = typeName.Substring(0, backtickIndex);
|
||||
}
|
||||
|
||||
builder.Append(typeName);
|
||||
builder.Append('<');
|
||||
|
||||
Type[] genericArgs = type.GetGenericArguments();
|
||||
for (int i = 0; i < genericArgs.Length; i++)
|
||||
{
|
||||
builder.Append(genericArgs[i].GetFriendlyName(isFullName));
|
||||
if (i < genericArgs.Length - 1)
|
||||
builder.Append(", ");
|
||||
}
|
||||
|
||||
builder.Append('>');
|
||||
return builder.ToString();
|
||||
}
|
||||
else if (type.IsArray)
|
||||
{
|
||||
return $"{type.GetElementType().GetFriendlyName()}[]";
|
||||
}
|
||||
else
|
||||
{
|
||||
return TypeMap.TryGetValue(type, out var alias) ? alias : isFullName ? type.FullName : type.Name; ;
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly Dictionary<Type, string> TypeMap = new Dictionary<Type, string>
|
||||
{
|
||||
[typeof(int)] = "int",
|
||||
[typeof(string)] = "string",
|
||||
[typeof(bool)] = "bool",
|
||||
[typeof(void)] = "void",
|
||||
[typeof(object)] = "object",
|
||||
[typeof(double)] = "double",
|
||||
[typeof(float)] = "float",
|
||||
[typeof(long)] = "long",
|
||||
[typeof(byte)] = "byte",
|
||||
[typeof(char)] = "char",
|
||||
[typeof(decimal)] = "decimal",
|
||||
[typeof(short)] = "short",
|
||||
[typeof(uint)] = "uint",
|
||||
[typeof(ulong)] = "ulong",
|
||||
[typeof(ushort)] = "ushort",
|
||||
[typeof(sbyte)] = "sbyte",
|
||||
};
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 字面量转为对应类型
|
||||
/// </summary>
|
||||
/// <param name="valueStr"></param>
|
||||
/// <returns></returns>
|
||||
public static Type ToTypeOfString(this string valueStr)
|
||||
{
|
||||
if (valueStr.IndexOf('.') != -1)
|
||||
{
|
||||
// 通过指定的类型名称获取类型
|
||||
return Type.GetType(valueStr);
|
||||
}
|
||||
|
||||
|
||||
if (valueStr.Equals("bool", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(bool);
|
||||
}
|
||||
#region 整数型
|
||||
else if (valueStr.Equals("sbyte", StringComparison.OrdinalIgnoreCase)
|
||||
|| valueStr.Equals(nameof(SByte), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(SByte);
|
||||
}
|
||||
else if (valueStr.Equals("short", StringComparison.OrdinalIgnoreCase)
|
||||
|| valueStr.Equals(nameof(Int16), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(Int16);
|
||||
}
|
||||
else if (valueStr.Equals("int", StringComparison.OrdinalIgnoreCase)
|
||||
|| valueStr.Equals(nameof(Int32), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(Int32);
|
||||
}
|
||||
else if (valueStr.Equals("long", StringComparison.OrdinalIgnoreCase)
|
||||
|| valueStr.Equals(nameof(Int64), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(Int64);
|
||||
}
|
||||
|
||||
else if (valueStr.Equals("byte", StringComparison.OrdinalIgnoreCase)
|
||||
|| valueStr.Equals(nameof(Byte), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(Byte);
|
||||
}
|
||||
else if (valueStr.Equals("ushort", StringComparison.OrdinalIgnoreCase)
|
||||
|| valueStr.Equals(nameof(UInt16), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(UInt16);
|
||||
}
|
||||
else if (valueStr.Equals("uint", StringComparison.OrdinalIgnoreCase)
|
||||
|| valueStr.Equals(nameof(UInt32), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(UInt32);
|
||||
}
|
||||
else if (valueStr.Equals("ulong", StringComparison.OrdinalIgnoreCase)
|
||||
|| valueStr.Equals(nameof(UInt64), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(UInt64);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 浮点型
|
||||
else if (valueStr.Equals("float", StringComparison.OrdinalIgnoreCase)
|
||||
|| valueStr.Equals(nameof(Single), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(Single);
|
||||
}
|
||||
else if (valueStr.Equals("double", StringComparison.OrdinalIgnoreCase)
|
||||
|| valueStr.Equals(nameof(Double), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(Double);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 小数型
|
||||
|
||||
else if (valueStr.Equals("decimal", StringComparison.OrdinalIgnoreCase)
|
||||
|| valueStr.Equals(nameof(Decimal), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(Decimal);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 其他常见的类型
|
||||
else if (valueStr.Equals(nameof(DateTime), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(DateTime);
|
||||
}
|
||||
|
||||
else if (valueStr.Equals(nameof(String), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return typeof(String);
|
||||
}
|
||||
#endregion
|
||||
|
||||
else
|
||||
{
|
||||
throw new ArgumentException($"无法解析的字面量类型[{valueStr}]");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user