引发System.InvalidCastException:源>中的至少一个元素.数组无法转换为目标数组类型. [英] throws System.InvalidCastException: At least one element in the source > array could not be cast down to the destination array type.

查看:197
本文介绍了引发System.InvalidCastException:源>中的至少一个元素.数组无法转换为目标数组类型.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,我使用以下代码将arraylist转换为数组,我的数组列表由bezier点的int值组成,代码如下:

sir i have use the following code for converting arraylist to array, my array list consist of int values of bezier points code is as below:

SqlCommand inkcmd;
cn = new SqlConnection("Data Source=localhost;Initial Catalog=amit;Integrated Security=True;Persist Security Info=True;Packet Size=4096");
cn.Open();
inkcmd = new SqlCommand("SELECT BPX,BPY FROM BezierTable where Alphabet= @Alphabet", this.sqlConnection1);
SqlParameter param = new SqlParameter();
param.ParameterName = "@Alphabet";
param.Value = txtImage.Text;
inkcmd.Parameters.Add(param);
inkcmd.Connection = cn;
                 
ArrayList rowList = new ArrayList();
rdr = inkcmd.ExecuteReader();
while (rdr.Read())
{
    object [] values = new object[rdr.FieldCount];
    rdr.GetValues(values);
    rowList.Add(values);
}
                      
Point[] y = (Point)rowList.ToArray(typeof(Point[]));/// here it throws error throws System.InvalidCastException: At least one element in the source > array could not be cast down to the destination array type.
            
Stroke newStroke = inkOverlay.Ink.CreateStroke(y);
newStroke.Move(2000,300);




关于

Googlejumbo




with regards

Googlejumbo

推荐答案

能够将对象投射到 ^ ],该对象最常创建为点结构.

您有一个对象数组,可能是Int32类型,无法将其强制转换为Point数组.

问候
Espen Harlinn
To be able to cast an object to a Point Structure[^], the object most be created as a Point Structure.

You have an array of object, possibly of type Int32, that''s not something you can cast to an array of Point.

Regards
Espen Harlinn


Espen的答案是正确的.现在,建议:

ArrayList已过时,因为它是在泛型之前创建的(v.2.0,而2.0之前的所有.NET Framework版本实际上是无用的,因此在所有新开发中实际上需要使用v.2.0或更高版本) .
另外,带有Type参数的方法ArrayList.ToArray太慢了,没有很好的用途,因为它需要检查每个元素的类型(正是导致代码失败的原因).

相反,请始终将System.Collection.Generic.List<>与您真正需要的类型一起使用,或将其与名称空间<code> System.Collection.Generic中的其他类型一起使用.
祝你好运,
—SA
Espen''s answer is correct. Now, suggestions:

The class ArrayList is obsolete, as it was created before generics (v.2.0, and all the versions of .NET Framework prior to 2.0 are practically useless, so you practically need to use v.2.0 or later in all new development).
Also, the method ArrayList.ToArray with Type parameters should be too slow without a good purpose, because it needs to check type on every element (exactly what caused a fail in your code).

Instead, always use System.Collection.Generic.List<> with the type you really need, or other types from the name space <code>System.Collection.Generic.

Good luck,
—SA



请检查您的数组列表值.可能是数组列表中的元素类型不同.

谢谢
Hi
Please check your array list values. May be elements type in the array list is different.

Thanks


这篇关于引发System.InvalidCastException:源&gt;中的至少一个元素.数组无法转换为目标数组类型.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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