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

前端 未结 1 1359
甜味超标
甜味超标 2021-01-20 06:22

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) vi

1条回答
  •  伪装坚强ぢ
    2021-01-20 06:53

    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.

    0 讨论(0)
提交回复
热议问题