using System; namespace XFEExtension.ArrayExtension { namespace AI { /// /// 对神经网络算法的数组矩阵的拓展 /// public static class MatrixOfArrayExtension { /// /// 以矩阵形式打印数组 /// /// /// public static void OutPutInMatrix(this T[] arrays) { int i; Console.Write("["); for (i = 0; i < arrays.GetLength(0) - 1; i++) { Console.Write($"{arrays[i]}\t"); } Console.Write($"{arrays[i]}]\n"); } /// /// 以矩阵形式打印数组 /// /// /// public static void OutPutInMatrix(this T[][] arrays) { for (int i = 0; i < arrays.GetLength(0); i++) { int j; Console.Write("["); for (j = 0; j < arrays.GetLength(1) - 1; j++) { Console.Write($"{arrays[i][j]}\t"); } Console.Write($"{arrays[i][j++]}]\n"); } } /// /// 以矩阵形式打印数组 /// /// /// public static void OutPutInMatrix(this T[,] arrays) { for (int i = 0; i < arrays.GetLength(0); i++) { int j; Console.Write("["); for (j = 0; j < arrays.GetLength(1) - 1; j++) { Console.Write($"{arrays[i, j]}\t"); } Console.Write($"{arrays[i, j++]}]\n"); } } } } /// /// 对数组的拓展 /// public static class ArrayExtension { /// /// 将数组转换为XFE格式字符串 /// /// /// /// public static string ToXFEString(this T[] arrays) { string str = string.Empty; foreach (var ary in arrays) { if (ary != 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 != 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(new string[] { "[+-", "-+]" }, 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; } } }