我正在尝试使用 Buffer.BlockCopy 将数组的一部分(data.item)复制到另一个数组中。如果我尝试一件事,我会收到关于无效内容的编译器错误(见下文),如果我使用强制转换,则会收到运行时错误。但如果我只分配它,它就可以正常工作:row[item.AspecName] = "random chars"。我在做什么错?
namespace ObjectListViewFramework
{
public partial class ObjectListviewForm : Form
{
public DataTable ListRows { get; private set; }
}
}
public void AddRows2(DLL_conduit data)
{
int columnIndex;
int bufferIndex = 0;
for (int rowIndex = 0; rowIndex < data.rowCount; rowIndex++)
{
DataRow row = ListRows.NewRow();
columnIndex = 0;
foreach (var item in objectListView1.AllColumns)
{
// this works dandy
row[item.AspectName] = "random chars";
// compiler error: the best overloaded method match has some invalid args
Buffer.BlockCopy(data.item, bufferIndex, row[item.AspectName], columnIndex * 256, 256);
// runtime error: invalid cast
Buffer.BlockCopy(data.item, bufferIndex, (char[])row[item.AspectName], columnIndex * 256, 256);
++columnIndex;
bufferIndex += 256;
}
ListRows.Rows.Add(row);
}
objectListView1.AddObjects(ListRows.DefaultView);
}