gpt4 book ai didi

c# - 需要帮助将 C# 转换为 vb。 "array initializer is missing 1 elements"

转载 作者:太空宇宙 更新时间:2023-11-03 17:11:53 26 4
gpt4 key购买 nike

我正在尝试使用 3.5 框架将以下代码从 C# 转换为 Vb。

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

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

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

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

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

我遇到问题并遇到相同错误的另一行是

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

我在 vb 中试过了,但它不起作用。

control = DirectCast(mi.Invoke(Me.Page, New Object(2) {ucType, Nothing}), Control)

ucType定义如下

Dim ucType As Type = Type.[GetType](typeName(1), True, True)

如有任何帮助,我们将不胜感激。

最佳答案

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

话虽如此,您问题的真正解决方案是让编译器计算出数组长度,如下所示:

Dim mi As MethodInfo = GetType(Page).GetMethod("LoadControl", New Type() {GetType(Type), GetType(Object())})

关于c# - 需要帮助将 C# 转换为 vb。 "array initializer is missing 1 elements",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/820553/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com