3 回答

TA貢獻(xiàn)1858條經(jīng)驗 獲得超8個贊
您至少需要兩行,一行用于聲明結(jié)果數(shù)組,另一行用于實際復(fù)制數(shù)據(jù)。
您可以首先將數(shù)組數(shù)組展平為單個數(shù)組,然后使用Buffer.BlockCopy將所有數(shù)據(jù)復(fù)制到結(jié)果數(shù)組。
下面是一個例子:
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");
結(jié)果:
如果您堅持要花哨:您可以BlockCopy
使用委托動態(tài)調(diào)用該委托返回,Object
以便您可以將其用于匿名類的分配,這顯然符合您的規(guī)則精神,并將所有內(nèi)容包裝到一個集合中,以便您以這樣的單行怪物結(jié)尾:
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貢獻(xiàn)1829條經(jīng)驗 獲得超9個贊
如果您被允許使用Func<>和 lambda,您當(dāng)然可以這樣做并進(jìn)行通用擴(kuò)展來轉(zhuǎn)換您正在調(diào)用它的對象。
/// <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);
}
有了這個,您應(yīng)該能夠通過執(zhí)行以下操作將 int[,] 轉(zhuǎn)換為 int[][]:
int[][] output = input.ForThis<int[][], int[,]>((obj) =>
{
// transform obj == input of type int[,] into int[][]
throw new NotImplementedException();
});
雖然我承認(rèn)這個解決方案真的感覺像是在作弊,因為您只是將多行轉(zhuǎn)換包裝到 lambda 中。
- 3 回答
- 0 關(guān)注
- 187 瀏覽
添加回答
舉報