我正在读取plc标签的值
public void synch_read() //reads device
{
Array values;
Array errors;
object qualities = new object(); //opc server will store the quality of the item
object timestamps = new object(); //store the timestamp of the read
//read directly from device
oGroup.SyncRead((short)OPCAutomation.OPCDataSource.OPCDevice, 2, ref handles, out values, out errors, out qualities, out timestamps);
String abcd = (Int16[])qualities.ToString();
}
在这一行
String abcd = ((Int16[])qualities).ToString();
我收到错误
unable to cast object of type 'system.int16[*]' to type 'system.Int16[]'
如何解决此错误?
修改
我试过
Int16[] abcd = (Int16[2])qualities;
错误; expected
答案 0 :(得分:2)
system.int16[*]
是一个多维数组,而不是单维数组。
Array array = (Array)qualities;
int dimensions = array.Rank;
如果dimensions
为2,那么它是int[,]
。如果它是3则为int[,,]
,依此类推。
使用foreach
迭代数组时请参阅https://stackoverflow.com/a/2893367/613130