From b5326160b937ea503c7e567aa43a289021b8a68a Mon Sep 17 00:00:00 2001 From: Martin Date: Tue, 18 Jun 2024 15:48:10 +0200 Subject: [PATCH] Fix untyped Array.Copy() for multidimensional arrays --- .../Extensions/NonGenericArray.cs | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Aardvark.Base/Extensions/NonGenericArray.cs b/src/Aardvark.Base/Extensions/NonGenericArray.cs index 92553254..a1e34c0c 100644 --- a/src/Aardvark.Base/Extensions/NonGenericArray.cs +++ b/src/Aardvark.Base/Extensions/NonGenericArray.cs @@ -7,6 +7,7 @@ public static class NonGenericArrayExtensions { #region Non-Generic Array + [Obsolete] private static readonly Dictionary> CopyFunMap = new Dictionary> { @@ -44,12 +45,25 @@ public static class NonGenericArrayExtensions { typeof(V4d[]), a => ((V4d[])a).Copy() }, }; - public static Array Copy(this Array array) - { - return CopyFunMap[array.GetType()](array); - } + /// + /// Creates a copy of the array. + /// + public static Array Copy(this Array array) + { + var counts = new long[array.Rank]; + + for (int i = 0; i < counts.Length; i++) + { + counts[i] = array.GetLongLength(i); + } + + var result = Array.CreateInstance(array.GetType().GetElementType(), counts); + Array.Copy(array, result, array.LongLength); + return result; + } - private static readonly Dictionary> CopyFunFunMap = + [Obsolete] + private static readonly Dictionary> CopyFunFunMap = new Dictionary> { { typeof(byte[]), (a, f) => ((byte[])a).Map((Func)f) }, @@ -86,7 +100,8 @@ public static Array Copy(this Array array) { typeof(V4d[]), (a, f) => ((V4d[])a).Map((Func)f) }, }; - public static Array Copy(this Array array, + [Obsolete("Anybody using this?")] + public static Array Copy(this Array array, Func funOfElementTypeToElementType, Func defaultFun = null) {