如何将System.Object [*]转换为System.Object [] II [英] How to cast System.Object[*] to System.Object[] II

查看:105
本文介绍了如何将System.Object [*]转换为System.Object [] II的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在他的问题中遇到了类似Cheva的问题:

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

如何将System.Object [*]转换为System.Object []

I通过COM互操作功能使用外部库(路透社EIKON API)。

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

提交请求后,对象将更新,其数据成员也将更新。对象目录为Data成员显示了以下内容:

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; }

在调试模式下,我看到,在提交请求后,DataStatus更改为dataset_full,

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

数据成员显示为{object [1..31]},我从调试下拉菜单中看到

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.

我的问题是我无法访问该对象。我可以将其转换为 object [] string [] 或其他任何形式。我什至找不到它的类型。

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.

总是说 System.Object [*] 无法转换为 System。 Object []

如果尝试使用.Type()找出类型,则会得到 System.Reflection.TargetInvocationException。我什至无法访问它的长度,这也给我错误

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?

推荐答案

System.Object [*] 是一个 System.Array ,它是一维的,但没有从 0 开始的索引。 C#不太支持它,因为C#使用基于0的索引。

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 [*] 不是 object [] ,因此不能转换为该类型。

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

可以使用:

var dataAsArray = (Array)Data;

var dataAsNonGenericIList = (IList)Data;

然后您可以 foreach

Then you can foreach through it.

或者您也可以按索引访问单个条目,例如:

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

object first = dataAsArray.GetValue(1);

分别:

object first = dataAsNonGenericIList[1];

您需要计算上下限( dataAsArray.GetLowerBound (0) dataAsArray.GetUpperBound(0)),但似乎下限是 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.

您可以按照通常的方式将字符串转换为(string)first (如果您每次都希望它是一个字符串),或者像 first as string 之类的东西(然后检查是否为空),如果您想要支持其他对象。

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.

如果您想复制怪异数组转换为0索引数组,请参见链接的标题中没有数字 II 的线程。索引当然会改变。例如 object [] newArray =新对象[dataAsArray.Length]; dataAsArray.CopyTo(newArray,0);

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

很多以后的编辑:

除了我上面写的内容外,C# dynamic System.Object [*] 时的$ c>功能。这也可以从上面的问题中推断出来。例如(我创建的数组排名第一,唯一维度的长度为 31 ,该维度的下限为 1 (即非零索引)):

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!

无论哪种情况,都会抛出 InvalidCastException

In either case an InvalidCastException is thrown:


无法将类型为'System.Object [*]'的对象转换为类型为'System.Object []'。

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.

同样的错误发生在我直接从 dynamic 投射到 Array IList ,就像我在上面的原始答案中所做的一样。

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;

和:

var dataAsNonGenericIList = (IList)(object)Data;

使用后期绑定(即绑定到对象投射)来自 dynamic 的代码似乎不受此错误的影响。下一个强制转换,从 object Array (或到 IList ),是正常的向下转换,没有动态魔术,当然可以。

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.

我只是意识到 RoadBump在链接线程中给出的答案也提供了此信息,因此请对该答案进行投票。

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

这篇关于如何将System.Object [*]转换为System.Object [] II的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆