.NET数组与下界> 0 [英] .Net arrays with lower bound > 0

查看:93
本文介绍了.NET数组与下界> 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管也许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

这篇关于.NET数组与下界> 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆