【发布时间】:2013-01-31 21:33:13
【问题描述】:
我正在尝试延迟加载(带收益返回的扩展)二维对象数组中的行。我收到以下错误:
c# 无法将“d__6”类型的对象转换为“System.Object[]”类型。
异常发生在Parse方法中发现的这一行:
yield return (TSource) conversion(o);
我不明白为什么C# 认为返回值是<>d__6 而不是Object[]。我通常在VB.NET 中编程,所以我不认为我理解C# 的细微差别。我究竟做错了什么?我查看了其他类似的问题/答案,但仍然感到困惑。
public static IEnumerable<TSource> Parse<TSource>(this object[,] array
, Func<IEnumerable<object[]>, IEnumerable<TSource>> conversion
, int rowStart, int columnStart, int rowCount, int columnCount)
{
IEnumerable<object[]> o
= array.ForEachRow(rowStart, columnStart, rowCount, columnCount);
yield return (TSource) conversion(o);
}
ForEachRow 方法:
public static IEnumerable<object[]> ForEachRow(this object[,] array,
int rowStart, int columnStart, int rowCount, int columnCount)
{
object[] array1d=new object[columnCount];
for (int row = rowStart; row < rowCount; row++)
{
for (int column = columnStart; column < columnCount; column++)
{
array1d[column] = array[row, column];
}
yield return (object[]) array1d;
}
}
我知道以前有人问过这个问题,但不幸的是,其他答案对我来说没有意义(我主要在 VB 中编程)。
可用于编译和测试的代码(在 VB.NET 中):
基本上,我从 Excel 中获取了一个 2D 对象数组,并希望将其放入一个类中并使用 Linq 进行评估。
Dim oData As Object(,) = {{"OrderDate", "Region", "Rep", "Item", "Units", "Unit Cost", "Total"} _
, {New DateTime(2011, 1, 6), "East", "Jones", "Pencil", 95, 1.99, 189.05} _
, {New DateTime(2011, 1, 23), "Central", "Kivell", "Binder", 50, 19.99, 999.5} _
, {New DateTime(2011, 2, 9), "Central", "Jardine", "Pencil", 36, 4.99, 179.64} _
, {New DateTime(2011, 2, 26), "Central", "Gill", "Pen", 27, 19.99, 539.73} _
, {New DateTime(2011, 3, 15), "West", "Sorvino", "Pencil", 56, 2.99, 167.44} _
}
Dim clsSales = oData.Parse(Of SaleOrder)(Function(o As Object()) New SaleOrder( _
If(IsDate(o(0)), o(0), #1/1/1900#) _
, o(1).ToString _
, o(2).ToString _
, o(3).ToString _
, If(IsNumeric(o(4)), CInt(o(4)), 0) _
, If(IsNumeric(o(5)), o(5), 0) _
), 1, 0, oData.GetUpperBound(0), 6)
For Each cls In clsSales
Console.WriteLine(cls.ToString)
Next
班级在哪里:
Class SaleOrder
Public Sub New(ByVal date_ As Date, ByVal region_ As String, ByVal rep As String, ByVal item_ As String, ByVal units As Integer _
, ByVal cost As Double)
OrderDate = date_
Region = region_
Representative = rep
Item = item_
UnitCount = units
UnitCost = cost
End Sub
Public OrderDate As DateTime
Public Region As String
Public Representative As String
Public Item As String
Public UnitCount As Integer = 5
Public UnitCost As Double
Public ReadOnly Property Total() As Double
Get
Return UnitCount * UnitCost
End Get
End Property
Public Overrides Function ToString() As String
Return String.Format("{0} {1} {2} {3} {4} {5} {6}", OrderDate, Region, Representative, Item, UnitCount, UnitCost, Total)
End Function
End Class
最终解决方案
public static IEnumerable<TSource> Parse<TSource>(this object[,] array
, Func<object[], TSource> conversion
, int rowStart, int columnStart, int rowCount, int columnCount)
{
for (int row = rowStart; row < rowCount; row++)
{
object[] array1d = new object[columnCount];
for (int column = columnStart; column < columnCount; column++)
{
array1d[column] = array[row, column];
}
yield return conversion(array1d);
}
}
【问题讨论】:
-
在哪一行出现异常?
-
如果你能提供一个简短但完整的例子来说明问题,那真的很有帮助。这里发生了很多事情,我们甚至不知道
TSource是什么,也不知道您的转换代表做什么。也很不清楚为什么你将array1d转换为object[],而这已经是它的类型...... -
好的,用我得到错误的代码行更新并删除了一些不相关的代码。
-
您遇到的错误是在编译时还是运行时?您一直将其称为异常,但异常仅在运行时发生;编译器错误发生在编译时。
-
请注意,在
ForEachRow中,您会一遍又一遍地产生相同的数组,同时对其进行变异。你……可能不应该那样做。您应该将array1d的创建移到第一个for循环内。
标签: c# static yield-return