无法转换对象类型“System.String [*]'输入'System.String []' [英] Unable To Cast object type 'System.String[*]' to type 'System.String[]'

查看:261
本文介绍了无法转换对象类型“System.String [*]'输入'System.String []'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我曾与一个code在C#.NET中的问题,我使用的DLL连接到OPC服务器被用在VB.NET工程,并与一点问题都没有的DLL。

我想显示一个列表框可用服务器的列表,在VB.NET(及工程)中使用的code是这一个:

 昏暗AllOPCServers作为对象
AllOPCServers = AnOPCServer.GetOPCServers

加载返回到供用户选择列表框中的列表
昏暗我短
对于i = LBOUND(AllOPCServers)为UBound函数(AllOPCServers)
    AvailableOPCServerList.Items.Add(AllOPCServers(I))
接下来我
 

和我写这在C#应用程序使用

 尝试
{
    变种_listOPCServer = _OPCServer.GetOPCServers();
    的foreach(在_listOPCServer变种I)
    {
        字符串的serverName =(字符串)我;
        listServers.Items.Add(服务器名);
    }
}
赶上(例外EXC)
{
    lstMsg.Items.Add(DateTime.Now +错误人Obtener利斯塔日OPC的:+ exc.Message);
}
 

在局部标签调试模式显示了这一点:

_listOPCServer | {字符串[1..2]} |动态{字符串[]} |

  [1] | Server01上|串
[2] | SERVER02|串
 

更新:

我得到的错误路线的foreach(VAR我在 _listOPCServer

无法转换对象类型System.String [*]'输入'System.String []

这是实际的错误。

我敢肯定,我做错了什么,有人可以帮我吗?

解决方案

VB.NET是多了很多精明的处理不符合要求的数组类型。不要犹豫,使用它,.NET可以很容易地有语言与对方进行互操作。

目前的问题是,OPC是一种基于COM的标准。您正在使用的服务器返回一个SAFEARRAY有不符合要求的下限,第一个指数是1,不为0并非不寻常的COM,0和1之间选择作为第一个数组索引是像端问题或争论是否西红柿是一种水果或蔬菜(它是一种水果,和little-endian是合适的蔬菜)。<​​/ P>

不过,0作为下界是什么C#坚持在与一维数组处理上。它想要一个载体,即始终有一个维度的下限为0高度优化的.NET运行库鲜明的数组类型。你获得了不匹配,以便被映射到的多维的阵列的一个的尺寸。不是数组类型,你可以前preSS在C#语言。

您的可以的本事在C#中,但你必须使用Array明确类型。事情是这样的,规定了对净度:

 阵列服务器=(阵列)_OPCServer.GetOPCServers();
INT第一= servers.GetLowerBound(0);
INT最后= servers.GetUpperBound(0);
对于(INT九=第一;九&LT; =最后++九){
    VAR的elem =(字符串)servers.GetValue(九);
    // 等等..
}
 

Hi everyone i had a issue with a code in C# .NET, I'm using a DLL for connect to OPC Servers the DLL that was used in a VB.NET project and works with no problem at all.

I'm trying to show a list of available Servers in a ListBox, the code used in VB.NET (and works) is this one:

Dim AllOPCServers As Object
AllOPCServers = AnOPCServer.GetOPCServers

' Load the list returned into the List box for user selection
Dim i As Short
For i = LBound(AllOPCServers) To UBound(AllOPCServers)
    AvailableOPCServerList.Items.Add(AllOPCServers(i))
Next i

and i wrote this to use in the C# application

try
{
    var _listOPCServer = _OPCServer.GetOPCServers();
    foreach(var i in _listOPCServer)
    {
        string serverName = (string)i;
        listServers.Items.Add(serverName);
    }             
}
catch (Exception exc)
{
    lstMsg.Items.Add(DateTime.Now + " Error al Obtener Lista de OPC's: " + exc.Message);
}

On Debug mode on Local tab shows this:

_listOPCServer | {string[1..2]} | dynamic {string[]} |

[1]        |  "Server01"    | string  
[2]        |  "Server02"    | string

UPDATE:

I get the error in line "foreach(var i in _listOPCServer)"

Unable To Cast object type 'System.String[*]' to type 'System.String[]'

That is the actually error.

I'm sure that I'm doing something wrong, can someone help me?

解决方案

VB.NET is a lot more astute at dealing with non-conforming array types. Don't hesitate to use it, .NET make it easy to have languages inter-operate with each other.

At issue is that OPC is a COM based standard. The server you are using is returning a SAFEARRAY with a non-conforming lower bound, the first index is 1. Not 0. Not that unusual in COM, choosing between 0 and 1 as the first array index is like the endian problem or arguing whether a tomato is a fruit or a vegetable (It is a fruit. And little-endian is the right kind of vegetable).

However, 0 as the lower bound is what C# insists on when dealing with one-dimensional arrays. It wants a "vector", a distinct array type that always has one dimension with a lower bound of 0. Heavily optimized in the .NET runtime. What you get back doesn't match so is mapped to a multi-dimensional array with one dimension. Not an array type you can express in the C# language.

You can hack it in C#, but you'll have to use the Array type explicitly. Something like this, spelled out for clarity:

Array servers = (Array)_OPCServer.GetOPCServers();
int first = servers.GetLowerBound(0);
int last = servers.GetUpperBound(0);
for (int ix = first; ix <= last; ++ix) {
    var elem = (string)servers.GetValue(ix);
    // etc..
}

这篇关于无法转换对象类型“System.String [*]'输入'System.String []'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆