c#无法转换'system.int16 [*]'类型的对象来输入'system.Int16 []'

时间:2016-03-10 06:21:50

标签: c# opc

我正在读取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

1 个答案:

答案 0 :(得分:2)

system.int16[*]是一个多维数组,而不是单维数组。

Array array = (Array)qualities;
int dimensions = array.Rank;

如果dimensions为2,那么它是int[,]。如果它是3则为int[,,],依此类推。

使用foreach迭代数组时请参阅https://stackoverflow.com/a/2893367/613130