简体   繁体   English

无法将对象类型“ System.String [*]”转换为类型“ System.String []”

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

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. 大家好,我在使用C#.NET中的代码时遇到问题,我正在使用DLL来连接OPC服务器,该DLL在VB.NET项目中使用并且完全没有问题。

I'm trying to show a list of available Servers in a ListBox, the code used in VB.NET (and works) is this one: 我试图在列表框中显示可用服务器的列表,在VB.NET中使用的代码(有效)是这样的:

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 我写了这个要在C#应用程序中使用

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 | _listOPCServer | {string[1..2]} | {string [1..2]} | dynamic {string[]} | 动态{string []} |

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

UPDATE: 更新:

I get the error in line "foreach(var i in _listOPCServer )" 我在“ foreach( _listOPCServer中的 var i)”行中收到错误

Unable To Cast object type 'System.String[*]' to type 'System.String[]' 无法将对象类型“ System.String [*]”转换为类型“ System.String []”

That is the actually error. 那是实际的错误。

I'm sure that I'm doing something wrong, can someone help me? 我确定我做错了什么,有人可以帮我吗?

Ok i found a way to work this out and it's only a mod of your advices 好的,我找到了解决此问题的方法,但这只是您的建议的一部分

 Array _listOPCServer = (Array)(object)_OPCServer.GetOPCServers();               

            foreach(object i in _listOPCServer)
            {
                string serverName = (string)i;
                listServers.Items.Add(serverName);
            }             

I only added (object) in the declaration and works fine, now i can show the list of servers just the way i wanted it 我只在声明中添加了(对象)并且可以正常工作,现在我可以按照我想要的方式显示服务器列表

or also do this 或者也这样做

Array _listOPCServer = (Array)(object)_OPCServer.GetOPCServers();               

            foreach(object i in _listOPCServer)
            {
                listServers.Items.Add(i);
            }

Again, thanks a lot for your help and time! 再次感谢您的帮助和时间!

VB.NET is a lot more astute at dealing with non-conforming array types. VB.NET在处理不合格的数组类型方面更加精明。 Don't hesitate to use it, .NET make it easy to have languages inter-operate with each other. 不要犹豫使用它,.NET使语言之间的互操作变得容易。

At issue is that OPC is a COM based standard. 问题在于OPC是基于COM的标准。 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). 您正在使用的服务器返回的SAFEARRAY下界不一致,第一个索引为1。不是0。在COM中并不罕见,在0和1之间选择第一个数组索引就像是endian问题或争论是否番茄是一种水果或蔬菜(它是一种水果。小尾数是正确的蔬菜)。

However, 0 as the lower bound is what C# insists on when dealing with one-dimensional arrays. 但是,C#在处理一维数组时坚持使用0作为下限。 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. 它需要一个“向量”,这是一种独特的数组类型,始终具有一维且下界为0。在.NET运行时中进行了优化。 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. 您无法使用C#语言表达数组类型。

You can hack it in C#, but you'll have to use the Array type explicitly. 可以使用C#修改它,但必须显式使用Array类型。 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..
}

While Hans is correct in distinguishing the difference between a zero-based array and a non-zero-based array, I still don't see why you're getting that exception. 虽然Hans区分基于零的数组和基于非零的数组之间的区别是正确的,但我仍然不明白为什么会出现该异常。

My guess is that you're declaring _OPCServer as dynamic , which defers type-binding until run-time. 我的猜测是您将_OPCServer声明为dynamic ,这将类型绑定推迟到运行时。 Thus _listOPCServer is dynamic as well. 因此, _listOPCServer也是dynamic

Since you're iterating over the array and extracting string s, the compiler may be trying to cast the object to a string[] which as Hans points out is invalid. 由于您要遍历数组并提取string s,因此编译器可能正试图将对象转换为string[] ,正如Hans指出的那样,该string[]无效。

You should be able to cast _listOPCServer to an Array and use the foreach just as you are: 您应该能够将_listOPCServer转换为Array并按_listOPCServer使用foreach

Array _listOPCServer = (Array)(_OPCServer.GetOPCServers());
foreach(var i in _listOPCServer)
{
    // etc.

暂无
暂无

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

相关问题 无法将类型“ System.String”强制转换为类型“ System.Object” - Unable to cast the type 'System.String' to type 'System.Object' 无法将“System.String”类型的对象转换为“System.Collections.Generic.List`1[System.String]”类型 - Unable to cast object of type 'System.String' to type 'System.Collections.Generic.List`1[System.String]' C# 中的 MS Word 自动化 - 无法将类型为“System.String[*]”的 object 转换为类型“System.String[]” - MS Word Automation in C# - Unable to cast object of type 'System.String[*]' to type 'System.String[]' 无法将类型类的对象强制转换为“ System.String”类型 - Unable to cast object of type class to type 'System.String' 无法将“DapperRow”类型的对象转换为“System.String”类型 - Unable to cast object of type 'DapperRow' to type 'System.String' 无法将“System.string”类型的对象转换为“....Event”类型 - Unable to cast object of type 'System.string' to type '....Event' 无法将“AttachmentDetailsDto”类型的对象转换为“System.String”类型 - Unable to cast object of type 'AttachmentDetailsDto' to type 'System.String' 无法将类型为“ System.String”的对象转换为类型为“ FileItem”的对象 - Unable to cast object of type 'System.String' to type 'FileItem' 如何解决“无法在CallBimlScript中强制转换&#39;System.String&#39;类型的对象” - How to troubleshoot “Unable to cast object of type 'System.String'” in CallBimlScript 无法将类型为“ TextmlBatchDeleteDocument”的对象转换为“ System.String” - Unable to cast object of type 'TextmlBatchDeleteDocument' to 'System.String'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM