简体   繁体   中英

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.


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 to System.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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM