Skip to content

Commit

Permalink
数组实现#33
Browse files Browse the repository at this point in the history
重构并扩展 ValidHelper 类的方法

重构了 `ToStringArray` 方法为通用的 `ToArray<T>` 方法,并添加了 `converter` 参数用于类型转换。删除了原有的 `ToStringArray` 方法,改为调用新的 `ToArray<T>` 方法。新增了多个类型转换方法,如 `ToInt32Array`、`ToInt64Array`、`ToDoubleArray` 等,这些方法都调用了 `ToArray<T>` 方法。此外,新增了 `ToEnum<T>` 和 `ToObject<T>` 方法,用于将对象转换为枚举类型和指定的引用类型。
  • Loading branch information
Soar360 committed Sep 11, 2024
1 parent a2eddb5 commit b1c96a9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
30 changes: 19 additions & 11 deletions XCode/Common/ValidHelper.Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,33 @@ namespace XCode;
/// </remarks>
public partial class ValidHelper
{
/// <summary>
/// 将给定的对象转换为字符串数组。
/// </summary>
/// <param name="value">要转换的对象。</param>
public static String[]? ToStringArray(object? value)
private static T[]? ToArray<T>(Object? value, Func<object?, T> converter)
{
if (value is String[] arr) return arr;
if (value == null || Convert.IsDBNull(value)) return default;
if (value is IEnumerable<string> list) return list.ToArray();
if (value is string str) return new string[] { str };
if (value is T[] arr) return arr;
if (value is null || Convert.IsDBNull(value)) return default;
if (value is IEnumerable<T> list) return list.ToArray();
if (value is T v) return new T[] { v };
if (value is IEnumerable)
{
var ret = new List<String>();
var ret = new List<T>();
foreach (var item in (IEnumerable)value)
{
ret.Add(Convert.ToString(item));
ret.Add(converter.Invoke(item));
}
return ret.ToArray();
}
return default;
}
public static Int32[]? ToInt32Array(Object? value) => ToArray(value, ToInt32);
public static Int64[]? ToInt64Array(Object? value) => ToArray(value, ToInt64);
public static Double[]? ToDoubleArray(Object? value) => ToArray(value, ToDouble);
public static Boolean[]? ToBooleanArray(Object? value) => ToArray(value, ToBoolean);
public static DateTime[]? ToDateTimeArray(Object? value) => ToArray(value, ToDateTime);
public static String[]? ToStringArray(Object? value) => ToArray(value, ToString);
public static Byte[]? ToByteArray(Object? value) => ToArray(value, ToByte);
public static Decimal[]? ToDecimalArray(Object? value) => ToArray(value, ToDecimal);
public static Int16[]? ToInt16Array(Object? value) => ToArray(value, ToInt16);
public static UInt64[]? ToUInt64Array(Object? value) => ToArray(value, ToUInt64);
public static T[]? ToEnumArray<T>(Object? value) where T : struct => ToArray(value, ToEnum<T>);
public static T[]? ToObjectArray<T>(Object? value) where T : class => ToArray(value, ToObject<T>);
}
22 changes: 22 additions & 0 deletions XCode/Common/ValidHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,26 @@ public static UInt64 ToUInt64(object? value)
{
return Convert.ToUInt64(value);
}

/// <summary>
/// 转换为枚举
/// </summary>
public static T ToEnum<T>(object? value) where T : struct
{
if (value is T t) return t;
if (value is null || Convert.IsDBNull(value)) return default;
if (typeof(T).IsEnum)
{
if (value is string str) return (T)Enum.Parse(typeof(T), str, true);
}
return (T)value;
}

public static T? ToObject<T>(object? value) where T : class
{
if (value is T t) return t;
if (value is null || Convert.IsDBNull(value)) return default;
//这里怎么实现呢?
return default;
}
}

0 comments on commit b1c96a9

Please sign in to comment.