How to cast System.Object[*] to System.Object[] II

可紊 提交于 2019-12-20 02:32:26

问题


I have got a similar problem like Cheva had in his Question:

How to cast System.Object[*] to System.Object[]

I use an external library (Reuters EIKON API) via the COM interop functionality.

After submitting a request, the object gets updated and its data member gets updated. The Object catalogue shows this for the Data member:

public virtual dynamic Data { get; }

In the debug mode, I can see, that after submitting the request, DataStatus changes to dataset_full and the Data member is actually filled.

The data member is shown as {object[1..31]} and I can see from the debug dropdown menu, that there a actually strings in this collection.

My problem is that I can't access this object. I can cast it to object[] or string[] or anything. I can't even find out the type of it.

It always says System.Object[*] can not be cast to System.Object[]

if I try to find out the type by using .Type(), I get "System.Reflection.TargetInvocationException". I even cannot access the Length of it, this also gives me the error

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

Has anyone seen a similar error?


回答1:


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

A System.Object[*] is not an object[], so you can't cast to that type.

You can use:

var dataAsArray = (Array)Data;

or

var dataAsNonGenericIList = (IList)Data;

Then you can foreach through it.

Or you can access individual entries by index, like this:

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 cast to string in the 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 to a 0-indexed array, see the linked thread without the numeral II in the title. Indexes will change, of course. For example object[] newArray = new object[dataAsArray.Length]; dataAsArray.CopyTo(newArray, 0);


Much later edit:

In addition to what I wrote above, there is a bug in the run-time binder used with C# dynamic functionality when the actual type is System.Object[*]. This could also be inferred from the question above. As an example (the array I create has rank one, the length of the sole dimension is 31, and the lower bound in that dimension is 1 (i.e. not zero-indexed)):

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

In either case an InvalidCastException is thrown:

Unable to cast object of type 'System.Object[*]' to type 'System.Object[]'.

This is a bug, I would say. The members exist, and I did not ask to cast to that type, I asked for late-binding to members that exist.

The very same bug occurs at run-time when I cast directly from dynamic to Array or IList, as I did above in the original answer.

You can work around this bug in the following way:

var dataAsArray = (Array)(object)Data;

and:

var dataAsNonGenericIList = (IList)(object)Data;

Casting to object with late binding (i.e. from dynamic) does not appear to be affected by the bug. The the next cast, from object to Array (or to IList), is a normal down-cast with no dynamic magic, and that works of course.

With this, my answer has become useful (years later).

I am just realizing that an answer by RoadBump in a linked thread also gives this information, so go up-vote that answer.



来源:https://stackoverflow.com/questions/23366984/how-to-cast-system-object-to-system-object-ii

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!