.NET数组与下界> 0下界、数组、NET、GT

2023-09-03 16:22:43 作者:男人的心就像洋蔥

尽管也许bizare事情想做的事情,我需要建立在.net中的阵列,下限> 0,这起初似乎是可能的,使用:

Although perhaps a bizare thing to want to do, I need to create an Array in .Net with a lower bound > 0. This at first seems to be possible, using:

Array.CreateInstance(typeof(Object), new int[] {2}, new int[] {9});

生成所期望的结果(对象具有下界集到9的阵列)。但是,创建数组实例不能再传递到其他的方法希望对象[] 给我一个错误,说:

Produces the desired results (an array of objects with a lower bound set to 9). However the created array instance can no longer be passed to other methods expecting Object[] giving me an error saying that:

System.Object的[*] 不能转换成 System.Object的[] 。什么是数组类型这种差异,我怎么能解决这个?

System.Object[*] can not be cast into a System.Object[]. What is this difference in array types and how can I overcome this?

编辑:测试code =

test code =

Object x = Array.CreateInstance(typeof(Object), new int[] {2}, new int[] {9});
Object[] y = (Object[])x;

其中失败,:无法转换类型的对象System.Object的[*]'输入'System.Object的[]'

Which fails with: "Unable to cast object of type 'System.Object[*]' to type 'System.Object[]'."

我也想指出,这种做法的使用多个维度什么时候工作

I would also like to note that this approach DOES work when using multiple dimensions:

Object x = Array.CreateInstance(typeof(Object), new int[] {2,2}, new int[] {9,9});
Object[,] y = (Object[,])x;

,工作正常。

Which works fine.

推荐答案

这篇文章解释它: HTTP ://www.panopticoncentral.net/articles/950.aspx