为什么我在ArrayList上得到InvalidCastException? [英] Why I get InvalidCastException on ArrayList?

查看:92
本文介绍了为什么我在ArrayList上得到InvalidCastException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在第1页上有以下代码:



 ArrayList titles =  new  ArrayList(); 
ArrayList tagst = new ArrayList();
for int i = 0 ; hfc.Count; i ++)
{
// 此处用于获取关键字的代码并上传文本文件..没问题
ArrayList tags = new ArrayList();

titles.Add(GettitleAsString());
tagst.Add(tags);

}





我将两个arraysList移动​​到另一个页面使用session。


第二页上的




 ArrayList files =(ArrayList )会话[  files]; 
ArrayList tags =(ArrayList)Session [ tags];

for int i = 0 ; i < files.Count; i ++)
{
sb.Append( < h4> + files [i] + < / H4>中);
foreach (ArrayList a in 标签)
{
foreach string s in a)
{
sb.Append(s + );
}
}
}





当我想在arraylist中有一个单独的记录时,它工作正常,但是当我在ArrayList中有多个记录时,我得到

 InvalidCastException:无法  cast  object   类型'  System.String '在foreach上键入'System.Collections.ArrayList' 

(文件中的ArrayList a)

解决方案

replace

 tagst.Add(tags);  //  这里添加数组直接列为项目 



with

 tagst.AddRange(tags);  //  添加范围会将每个标记项添加到标记 


从不使用 ArrayList ,但支持已经运行的旧软件的情况除外。早在.NET Framework v.2.0引入遗传学时,那些非泛型(非专用)类型就已经过时了。请改用 System.Collections.Generic.List<> 和其他通用类型。它们不易出错,因为它们可以帮助您避免类型转换,而不会影响性能。请参阅: http://msdn.microsoft.com/en-us/library/system .collections.generic.aspx [ ^ ]。



-SA


I have the following code on page1:

ArrayList titles = new ArrayList();
ArrayList tagst= new ArrayList();
for(int i=0;hfc.Count;i++)
{
//Here code to pick up keywords and upload text files .. No problem in it 
ArrayList tags= new ArrayList();

titles.Add(GettitleAsString());
tagst.Add(tags);

}



I move the two arraysList then to another page using session.

on the page two:

ArrayList files = (ArrayList)Session["files"];
ArrayList tags = (ArrayList)Session["tags"];

for (int i = 0; i < files.Count; i++)
{
   sb.Append("<h4>" + files[i] + "</h4>");
   foreach (ArrayList a in tags)
                        {
                            foreach (string s in a)
                            {
                                sb.Append(s + " ");
                            }
                        }
}



When I want to have one single record in arraylist, it works normally, but when I have multiple records in ArrayList, I get

InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Collections.ArrayList'

on foreach (ArrayList a in files)

解决方案

replace

tagst.Add(tags);// here you adding array list directly as item


with

tagst.AddRange(tags);// add range will add each item of tags to the tagst


Never use ArrayList, except for the cases when you support legacy software which already works. Those non-generic (non-specialized) types became obsolete as early as of .NET Framework v.2.0 when the genetics were introduced. Use System.Collections.Generic.List<> and other generic types instead. They are less error-prone, because they help you to avoid that type casting, without compromising performance. Please see: http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx[^].

—SA


这篇关于为什么我在ArrayList上得到InvalidCastException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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