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

4
  • 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...) Commented Mar 8, 2016 at 11:20
  • Put if (asw is System.Collections.IEnumerable) before your foreach and check if you can enumerate Commented Mar 8, 2016 at 11:32
  • Yes it is, Surprisingly! Commented Mar 8, 2016 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. Commented Mar 8, 2016 at 16:10

4 Answers 4

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

Again Unhandled error, Unable to convert System.String[*] to System.String[]
can you post the full error. Also post your code not the screenshot.
0

In theorie, you could just do

string[] foo = asw;

and work with this.

1 Comment

It does not work. It gives and unhanded error. It is : Unable to convert System.String[*] to System.String[]
0

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

foreach(var item in asw.AsEnumerable())
{
}

1 Comment

AsEnumerable() is not defined for system.array. It has type:System.string[*}
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

4 Comments

Nice Idea, but not still working. Again the same Unhandled error.
@Amir_Controller what error says? Could you update your question with code and full error message?
ok, as long as you have it in as dynamic I updated the answer.
A Good step forward. Converting to Object it keeps the items, but converting to array it gives a null array. Marvelous!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.