您至少需要两行,一行用于声明结果数组,另一行用于实际复制数据.
您可以先将数组数组展平为单个数组,然后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");
Run Code Online (Sandbox Code Playgroud)
结果:
如果您坚持要花哨:您可以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;
Run Code Online (Sandbox Code Playgroud)