3 回答
![?](http://img1.sycdn.imooc.com/533e50ed0001cc5b02000200-100-100.jpg)
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;
![?](http://img1.sycdn.imooc.com/545863e80001889e02200220-100-100.jpg)
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 关注
- 166 浏览
添加回答
举报