0

我正在尝试使用 ArrayList,并将其绑定到 BindingList ...

如果我尝试将其放入(我有 Option Strict On),我会收到我实施的转换建议 -

然而,无论我尝试什么,我都会遇到运行时错误。

Unable to cast object of type 'ArrayList' to type 'IList`

编码:

Dim myBoundList As System.ComponentModel.BindingList(Of something) =
    New System.ComponentModel.BindingList(Of something)
      (CType(myArrayList, System.Collections.Generic.IList(Of something)))

我试图插入 .ToArray ...

链接https://stackoverflow.com/a/8770832/1217150上接受的答案完全相同(即使意图相反),我已经尝试过了......(我的意思是,创建一个 IList 项目, 并赋值

IList iList = new ArrayList();

它给了我同样的错误......

我正在使用 VB.NET,但 c# 也会有所帮助。请帮忙。谢谢你。

4

2 回答 2

1

您应该使用泛型List类(一种 .NET 类型)而不是使用非泛型ArrayList. 我怀疑这可能与您的问题有关。它可能试图将非泛型ArrayList转换为IList<T>(IList 的泛型版本)。

于 2012-09-14T02:31:53.437 回答
0

如果您尝试将数组列表作为项目添加到列表框中,可以使用以下代码:

private void Form1_Load(object sender, EventArgs e)
{
    // Create a new array list.
    ArrayList list = new ArrayList();

    // Add items to the array list
    list.Add("MyItem1");
    list.Add("MyItem2");
    list.Add("MyItem3");

    //Set the data source of the listbox as the array list
    listBox1.DataSource = list;
}
于 2012-09-14T03:34:52.383 回答