类型不符?
public static void DisplayByPrice(Book[] arrBooks)
{
int bookPrice = arrBooks.GetUpperBound(0) - arrBooks.GetLowerBound(0) + 1;
int [] lengths = new int [] { bookPrice };
int[] lowerBounds = new int[] { 0 };
Array arrPrices = Array.CreateInstance(Type.GetType("System.String"), lengths, lowerBounds);
for (int i = arrBooks.GetLowerBound(0); i <= arrBooks.GetUpperBound(0); i++)
arrPrices.SetValue(arrBooks[i].dblPrice,i);
Array.Sort(arrPrices, arrBooks);
foreach (Book item in arrBooks)
{
Console.WriteLine("{0} {1} {2}", item.dblPrice, item.strName, item.strAuthor);
}
}
系统提示arrPrices.SetValue(arrBooks[i].dblPrice,i);这里有错“未处理InvalidCastException”请问怎么改?
------解决方案--------------------Array arrPrices = Array.CreateInstance(Type.GetType("System.String"), lengths, lowerBounds);
改成
Array arrPrices = Array.CreateInstance(Type.GetType("System.Double"), lengths, lowerBounds);
------解决方案--------------------