C#中的System.String [*]和System.String []差异 [英] System.String[*] and System.String[] Difference in C#
问题描述
编辑:我知道C#等效于对于每个服务器作为服务器中的字符串
是 foreach(服务器)
,但由于 GetOPCServers
返回对象,因此应将其强制转换为可迭代类型。
我正在使用COM库开发应用程序。由于64位会导致问题,因此我的目标CPU是x86。
除 GetOPCServers()
之外,所有方法均按预期工作。 / p>
尽管使用相同dll的Visual Basic代码不会引起问题,但C#抛出 System.InvalidCastException
表示:
System.Core.dll
$ b $中发生类型'System.InvalidCastException'
的首次机会异常b附加信息:
'System.String [*]'türündekinesne'System.String []'türüneatılamadı。
对错误消息表示歉意,因为我的操作系统是土耳其语。
System.String [*]
类型的对象不能分配到System.String []
System之间的区别是什么.String [*]
和 System.String []
?
使用相同dll的VB代码无例外地运行
公共类OpcInfo
将Dim服务器作为VariantType
Dim服务器作为OPCAutomation.OPCServer =新的OPCAutomation.OPCServer()
函数GetServers()
Dim服务器作为对象
Dim _servers作为新列表(字符串)
服务器= server.GetOPCServers()
每个服务器作为字符串在服务器
_servers.Add(服务器)
下一个
返回服务器
结束函数
结束类
但是C#公司de也使用相同的DLL引发异常
静态void Main(string [] args)
{
var opc_servers = new OPCAutomation.OPCServer()。GetOPCServers();
foreach((字符串[]] opc_servers中的变量项)
{
Console.WriteLine(item);
}
}
更有趣的是,我能够查看监视/即时窗口中的数据:
错误代码 -2147467262
对应于 FFFFFFFF80004002
,并根据 https://technet.microsoft.com/en-us/library/bb632794.aspx
E_NOINTERFACE
FFFFFFFF80004002
-2147467262
不支持此类接口
感谢大家。
在循环中进行铸造并不能解决问题。
@pikoh在使用C#的MS Word自动化达到了目的:
还有 var opc_servers = ...
不起作用。必须是 object opc_servers = ...
object opc_servers = new OPCAutomation .OPCServer()。GetOPCServers();
var服务器= [[Array)(opc_servers));
for(int i = 1; i< = servers.Length; i ++)
{
Console.WriteLine((string)servers.GetValue(i ));
}
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.
I am developing an application using a COM library. Since 64bit causes problems, my target CPU x86.
All methods works as expected, except GetOPCServers()
.
Although Visual Basic code using same dll does not cause problem, C# throws System.InvalidCastException
saying that:
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[]
What is diffence between System.String[*]
and System.String[]
?
The VB code using same dll runs without exception
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
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:
The error code -2147467262
corresponds to FFFFFFFF80004002
and the explanation according to 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:
And also var opc_servers = ...
did not work. Must be 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));
}
这篇关于C#中的System.String [*]和System.String []差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!