需要帮助将C#转换为vb."数组初始值设定项缺少1个元素"

1 c# vb.net typeof gettype

我试图使用3.5框架将以下代码从C#转移到Vb.

这是C#中我遇到问题的代码.

MethodInfo mi = typeof(Page).GetMethod("LoadControl", new Type[2] { typeof(Type), typeof(object[]) });
Run Code Online (Sandbox Code Playgroud)

我认为在VB中会是这样的;

Dim mi As MethodInfo = GetType(Page).GetMethod("LoadControl", New Type(2) {GetType(Type), GetType(Object())})
Run Code Online (Sandbox Code Playgroud)

但我得到以下错误"数组初始化程序缺少1个元素"

我遇到麻烦并得到同样错误的另一条线是

control = (Control) mi.Invoke(this.Page, new object[2] { ucType, null });
Run Code Online (Sandbox Code Playgroud)

我在vb中尝试了这个,但它不起作用.

control = DirectCast(mi.Invoke(Me.Page, New Object(2) {ucType, Nothing}), Control)
Run Code Online (Sandbox Code Playgroud)

ucType定义如下

Dim ucType As Type = Type.[GetType](typeName(1), True, True)
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.

Kev*_*vin 6

VB.Net数组是基于0的,但是使用最高索引而不是项目数来声明.因此,索引为0..9的10项数组被声明为Item(9).

话虽如此,你的问题的真正解决方案是让编译器弄清楚数组长度,如下所示:

Dim mi As MethodInfo = GetType(Page).GetMethod("LoadControl", New Type() {GetType(Type), GetType(Object())})
Run Code Online (Sandbox Code Playgroud)


Ada*_*son 5

在VB.NET中,数组声明采用数组的上限,而不是像C#那样的长度(如果你问我那么傻).因此,您需要将传递到数组声明中的数量减少1(因为数组从零开始).