简体   繁体   English

C#无法将类型为'system.int16 [*]'的对象转换为类型为'system.Int16 []'的对象

[英]c# unable to cast object of type 'system.int16[*]' to type 'system.Int16[]'

I am reading the values from plc tags 我正在读取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();
}

In this line 在这条线

String abcd = ((Int16[])qualities).ToString();

I am getting the error 我收到错误

unable to cast object of type 'system.int16[*]' to type 'system.Int16[]'

How can I solve this error? 我该如何解决这个错误?

EDIT 编辑

I tried 我试过了

Int16[] abcd = (Int16[2])qualities;

error ; expected 错误; expected ; expected

The system.int16[*] is a multidimensional array, not a single dimensional array. system.int16[*]是一个多维数组,而不是一维数组。

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

If dimensions is 2, then it is a int[,] . 如果dimensions为2,则为int[,] If it is 3 it is int[,,] and so on. 如果为3,则为int[,,] ,依此类推。

For iterating the array with foreach see for example https://stackoverflow.com/a/2893367/613130 要使用foreach迭代数组,请参见例如https://stackoverflow.com/a/2893367/613130

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型为“ System.Int16”的对象转换为类型为“ System.String”的对象 - Unable to cast object of type 'System.Int16' to type 'System.String' 无法将类型为&#39;System.Int16&#39;的对象转换为类型为&#39;System.String&#39;-AsEnumerable()。ToDictionary - Unable to cast object of type 'System.Int16' to type 'System.String' - AsEnumerable().ToDictionary 可查询 <T> 。订购 <T, object> 抛出:无法将类型“ System.Int16”强制转换为类型“ System.Object” - IQueryable<T>.OrderBy<T, object> throws: Unable to cast the type 'System.Int16' to type 'System.Object' 类型&#39;System.Int16&#39;的对象不能转换为类型&#39;System.Nullable`1 [System.Int32] - Object of type 'System.Int16' cannot be converted to type 'System.Nullable`1[System.Int32] 从实例化的“ System.Int16”类型到“ System.String”类型的指定转换无效 - The specified cast from a materialized 'System.Int16' type to the 'System.String' type is not valid 实体框架核心 DbContext.SaveChanges 抛出 System.InvalidCastException:无法将 System.Boolean 类型的对象转换为 System.Int16 - Entity Framework Core DbContext.SaveChanges throws System.InvalidCastException: Unable to cast object of type System.Boolean to type System.Int16 &#39;System.Int16&#39;类型的表达式不能用于返回类型&#39;System.Object&#39; - Expression of type 'System.Int16' cannot be used for return type 'System.Object' Expression.AddChecked和System.Int16 - Expression.AddChecked and System.Int16 实体框架错误...属性无法设置为“System.Int16”值 - Entity framework error … property could not be set to 'System.Int16' value 具有MySql和Dot Net Core的IdentityServer4-在类型&#39;System.Int16&#39;和&#39;System.Boolean&#39;之间未定义强制运算符 - IdentityServer4 with MySql and Dot Net Core - No coercion operator is defined between types 'System.Int16' and 'System.Boolean'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM