3 回答

TA貢獻1858條經驗 獲得超8個贊
您至少需要兩行,一行用于聲明結果數組,另一行用于實際復制數據。
您可以首先將數組數組展平為單個數組,然后使用Buffer.BlockCopy將所有數據復制到結果數組。
下面是一個例子:
var source = new int[][] {
new int[4]{1,2,3,4},
new int[4]{5,6,7,8},
new int[4]{1,3,2,1},
new int[4]{5,4,3,2}
};
var expected = new int[4,4] {
{1,2,3,4},
{5,6,7,8},
{1,3,2,1},
{5,4,3,2}
};
var result = new int[4, 4];
// count = source.Length * source[0].Length * sizeof(int) = 64, since BlockCopy is byte based
// to be dynamically you could also use System.Runtime.InteropServices.Marshal.SizeOf(source[0][0]) instead of sizeof(int)
Buffer.BlockCopy(source.SelectMany(r => r).ToArray(), 0, result, 0, 64);
result.Dump("result");
expected.Dump("expected");
結果:
如果您堅持要花哨:您可以BlockCopy
使用委托動態調用該委托返回,Object
以便您可以將其用于匿名類的分配,這顯然符合您的規則精神,并將所有內容包裝到一個集合中,以便您以這樣的單行怪物結尾:
var result = new[]{ new int[4, 4] }.Select(x => new { r = x, tmp = Delegate.CreateDelegate(typeof(Action<Array, int, Array, int, int>), typeof(Buffer).GetMethod("BlockCopy")).DynamicInvoke(new Object[]{source.SelectMany(r => r).ToArray(), 0, x, 0, 64})}).First().r;

TA貢獻1829條經驗 獲得超9個贊
如果您被允許使用Func<>和 lambda,您當然可以這樣做并進行通用擴展來轉換您正在調用它的對象。
/// <typeparam name="T">Output type</typeparam>
/// <typeparam name="U">Calling type</typeparam>
/// <param name="obj">object to pipe</param>
/// <param name="func">blackbox function</param>
/// <returns>whatever</returns>
public static T ForThis<T,U> (this U obj, Func<U,T> func)
{
return func(obj);
}
有了這個,您應該能夠通過執行以下操作將 int[,] 轉換為 int[][]:
int[][] output = input.ForThis<int[][], int[,]>((obj) =>
{
// transform obj == input of type int[,] into int[][]
throw new NotImplementedException();
});
雖然我承認這個解決方案真的感覺像是在作弊,因為您只是將多行轉換包裝到 lambda 中。
- 3 回答
- 0 關注
- 182 瀏覽
添加回答
舉報