我试图使用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)
任何帮助将不胜感激.
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)