[英]System.String[*] and System.String[] Difference in C#
Edit : I know that C# equivalent of For Each server As String In servers
is foreach(var item in servers)
but since GetOPCServers
returns object, it should be cast to iterable type. 编辑 :我知道C#相当于
For Each server As String In servers
是foreach(var item in servers)
For Each server As String In servers
中的foreach(var item in servers)
但是由于GetOPCServers
返回对象,它应该被转换为可迭代类型。
I am developing an application using a COM library. 我正在使用COM库开发应用程序。 Since 64bit causes problems, my target CPU x86.
由于64bit导致问题,我的目标CPU x86。
All methods works as expected, except GetOPCServers()
. 除
GetOPCServers()
外,所有方法都按预期工作。
Although Visual Basic code using same dll does not cause problem, C# throws System.InvalidCastException
saying that: 虽然使用相同的DLL的Visual Basic代码不会导致问题,但C#抛出
System.InvalidCastException
说:
A first chance exception of type 'System.InvalidCastException'
occurred in System.Core.dll
Additional information:
'System.String[*]' türündeki nesne 'System.String[]' türüne atılamadı.
Apologizes for the error message, since my OS in Turkish. 为我的土耳其语操作系统提供错误消息道歉。
The object in type
System.String[*]
can not be assigned toSystem.String[]
System.String[*]
类型中的对象不能分配给System.String[]
What is diffence between System.String[*]
and System.String[]
? System.String[*]
和System.String[]
之间的差异是什么?
The VB code using same dll runs without exception 使用相同dll的VB代码无例外地运行
Public Class OpcInfo
Dim servers As VariantType
Dim server As OPCAutomation.OPCServer = New OPCAutomation.OPCServer()
Function GetServers()
Dim servers As Object
Dim _servers As New List(Of String)
servers = server.GetOPCServers()
For Each server As String In servers
_servers.Add(server)
Next
Return _servers
End Function
End Class
But the the C# code also uses same dll throws exception 但是C#代码也使用相同的dll抛出异常
static void Main(string[] args)
{
var opc_servers = new OPCAutomation.OPCServer().GetOPCServers();
foreach (var item in (string[])opc_servers)
{
Console.WriteLine(item);
}
}
More interestingly, I able to view the data in Watch/Immediate windows: 更有趣的是,我能够在Watch / Immediate窗口中查看数据:
The error code -2147467262
corresponds to FFFFFFFF80004002
and the explanation according to https://technet.microsoft.com/en-us/library/bb632794.aspx 错误代码
-2147467262
对应于FFFFFFFF80004002
以及根据https://technet.microsoft.com/en-us/library/bb632794.aspx的说明
E_NOINTERFACE
FFFFFFFF80004002
-2147467262
No such interface supported
Thanks everyone. 感谢大家。
Casting inside the loop does not make the trick. 在循环内部进行投射并不成功。
as @pikoh stated the answer on MS Word Automation in C# made the trick: 正如@pikoh 在C#中对MS Word Automation的回答所说的那样:
And also var opc_servers = ...
did not work. 并且
var opc_servers = ...
也没有用。 Must be object opc_servers = ...
必须是
object opc_servers = ...
object opc_servers = new OPCAutomation.OPCServer().GetOPCServers();
var servers = ((Array)(opc_servers));
for (int i = 1; i <= servers.Length; i++)
{
Console.WriteLine((string)servers.GetValue(i));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.