1

enter image description hereI have c# project, in which a data type is unknown to me.

As shown in the picture, the data type is from a class which has no method or property, but when I hover over it, it shows the items.

Also, I get an error when I try the foreach command.

Also, it is not convertible to List or Dictionary or Stringp[]. Please guide me to work on it. Thanks

 private void button2_Click(object sender, EventArgs e)
    {
        var a1 = new OPCServer();

        var anOPCDAServers=new OPCServer();
        var serverName="FARATARH-PC";
        var AllOPCDAServers = new List<string>();
        var asw = anOPCDAServers.GetOPCServers("FARATARH-PC");
        //Console.WriteLine(typeof(asw).ToString);

        foreach (var item in asw)
        {
            //AllOPCDAServers.Add(item);
        }


        //var nod = AllOPCDAServers[4];


        //a1.Connect(serverName, );




    }

screenshot

  • 2
    Welcome to Stack Overflow. Please put code *as text* in your question, along with the error message *as text*. A screenshot can be useful for additional information, but the code should always be present as text, along with contextual descriptions (in this case, that you're talking to a COM object...) – Jon Skeet Mar 08 '16 at 11:20
  • Put `if (asw is System.Collections.IEnumerable)` before your `foreach` and check if you can enumerate – Mike Debela Mar 08 '16 at 11:32
  • Yes it is, Surprisingly! – Amir_Controller Mar 08 '16 at 11:38
  • This is not a repeated question. If so, the proposed answer would work for me. That person has been using a differenet dll. – Amir_Controller Mar 08 '16 at 16:10

4 Answers4

1

asw is an array of dynamic object. Dont use var type to loop instead use dynamic as the type.

foreach (dynamic item in aswItems)
{
   // code here
}
Carbine
  • 7,849
  • 4
  • 30
  • 54
0

In theorie, you could just do

string[] foo = asw;

and work with this.

Markus Nigbur
  • 409
  • 2
  • 6
0

Try casting AsEnumerable() which would allow you to enumerate the array.

foreach(var item in asw.AsEnumerable())
{
}
James Dev
  • 2,979
  • 1
  • 11
  • 16
0

String[*] indicates an array that can have a lower bound other than zero. They are incompatible with a regular string[], but you can still cast it to a System.Array and work with it that way. So try

IEnumerable<string> oOPCList;

oOpcServer = new RsiOPCAuto.OPCServer();
oOPCList = ((Array)(object)oOpcServer.GetOPCServers()).Cast<string>();
foreach(var item in oOPCList)
    ...

The strange cast first to object, then to Array, and then to IEnumerable<string> via Cast<string> is needed because of the following:

GetOPCServers returns a dynamic type. Trying to access that dynamic instance in any way - even via a call to GetType triggers an InvalidCastException. Therefore, it first needs to be cast to object so it no longer is a dynamic type. After that, we can cast it to an Array, the only supported way in C# to work with non-zero-based arrays. But Array is not strong typed, so we append the call to Cast<string> to get a strong typed enumerable.

source

Community
  • 1
  • 1
Ivan Leonenko
  • 2,363
  • 2
  • 27
  • 37