namespace XFEExtension.NetCore.ArrayExtension;
///
/// 对数组的拓展
///
public static class ArrayExtension
{
private static readonly string[] separator = ["[+-", "-+]"];
///
/// 将数组转换为XFE格式字符串
///
///
///
///
public static string ToXFEString(this T[] arrays)
{
string str = string.Empty;
foreach (var ary in arrays)
{
if (ary is not null)
str += $"[+-{ary?.ToString()?.Replace("[+", "[++").Replace("+]", "++]")}-+]";
}
return str;
}
///
/// 将数组转换为XFE格式字符串
///
///
///
/// 属性名称
///
public static string ToXFEString(this T[] arrays, string propertyName) where T : class
{
string str = string.Empty;
foreach (var ary in arrays)
{
if (ary is not null)
str += $"[+-{ary?.GetType()?.GetProperty(propertyName)?.GetValue(ary)?.ToString()?.Replace("[+", "[++").Replace("+]", "++]")}-+]";
}
return str;
}
///
/// 将XFE格式字符串转换为数组
///
///
///
/// T类型的数组
public static T[] ToXFEArray(this string str)
{
string[] strings = str.Split(separator, StringSplitOptions.None);
for (int i = 0; i < strings.Length; i++)
{
strings[i] = strings[i].Replace("[++", "[+").Replace("++]", "+]");
}
T[] array = new T[(strings.Length - 1) / 2];
for (int i = 0, j = 1; i < array.Length; i++, j += 2)
{
array[i] = (T)Convert.ChangeType(strings[j], typeof(T));
}
return array;
}
///
/// 获取数组中的类型
///
///
///
public static Type[]? GetTypes(this object[] objects)
{
if (objects is null)
return null;
Type[] types = new Type[objects.Length];
for (int i = 0; i < objects.Length; i++)
{
types[i] = objects[i].GetType();
}
return types;
}
}