How to pour System.Object [*] into System.Object [] II

I have a similar problem like Cheva in his Question:

How to load System.Object [*] into System.Object []

I use an external library (Reuters EIKON API) through the functionality of COM interoperability.

After sending the request, the object is updated and its data member is updated. The object directory shows this for the Data member:

public virtual dynamic Data { get; } 

In debug mode, I see that after sending the request, the DataStatus changes to dataset_full, and the data item is actually populated.

The data item is shown as {object [1..31]}, and in the debug drop-down menu you can see that there are rows in this collection.

My problem is that I cannot access this object. I can use it for object[] or string[] or whatever. I can’t even find out his type.

He always says that System.Object[*] cannot be attributed to System.Object[]

if I try to figure out the type using .Type (), I get a "System.Reflection.TargetInvocationException". I can’t even access its length, it also gives me an error

 System.InvalidCastException -> Object of Type "System.Object[*]" cannot be cast to type "System.Object[]" 

Has anyone seen a similar error?

+3
casting c # com-interop
source share
1 answer

System.Object[*] is a System.Array that is one-dimensional but not indexed starting at 0 . It is not supported in C # because C # uses 0-based indexing.

A System.Object[*] not an object[] , so you cannot use this type.

You can use:

 var dataAsArray = (Array)Data; 

or

 var dataAsNonGenericIList = (IList)Data; 

Then you can foreach .

Or you can access individual records by index, for example:

 object first = dataAsArray.GetValue(1); 

respectively:

 object first = dataAsNonGenericIList[1]; 

You will need to work out the upper and lower bounds ( dataAsArray.GetLowerBound(0) , dataAsArray.GetUpperBound(0) ), but it seems that the lower bound is 1 in your case.

You can use string usual ways, (string)first if you expect it to be a string every time, or something like first as string (then check for null) if you want to support other objects.


If instead you want to copy the "weird" array into an array with index 0, see the associated stream without the number II in the header. Of course, the indices will change. For example, object[] newArray = new object[dataAsArray.Length]; dataAsArray.CopyTo(newArray, 0); object[] newArray = new object[dataAsArray.Length]; dataAsArray.CopyTo(newArray, 0);


More details :

In addition to what I wrote above, the C # dynamic function is used in the runtime binding when the actual type is System.Object[*] . This could also be done from the above question. As an example (the array I create has a rank of one, the length of a single dimension is 31 , and the lower bound in this dimension is 1 (i.e., Not zero indexing)):

 dynamic Data = Array.CreateInstance(typeof(object), new[] { 31, }, new[] { 1, }); dynamic t = Data.GetType(); // throws! dynamic s = Data.ToString(); // throws! 

In any case, an InvalidCastException is InvalidCastException :

Cannot pass an object of type "System.Object [*]" to enter "System.Object []".

This is a mistake, I would say. Members exist, and I did not ask to attach to this type, I asked to be late with existing members.

The same error occurs at runtime when I execute directly from dynamic to Array or IList , as I did above in the original answer.

You can get around this error as follows:

 var dataAsArray = (Array)(object)Data; 

and

 var dataAsNonGenericIList = (IList)(object)Data; 

Drop on object with late binding (i.e. from dynamic ) does not affect the error. The next cast, from object to Array (or to IList ), is a normal application without dynamic magic, and it works, of course.

At the same time, my answer became useful (after years).

I just understand that the RoadBump response in the linked thread also gives this information, so go to the vote to respond.

+1
source share

All Articles