Access Violation: Cannot apply indexing with [] to an expression of type 'object'

一世执手 提交于 2020-06-17 03:09:07

问题


I have the following variable: _Data.

The variable contains the following info: _Data Variable

How can i access the message field?

I tried _Data["messages"][0] - but it not wokring.

I recieved the following error: Cannot apply indexing with [] to an expression of type 'object'

What am i doing wrong?

Thanks.


回答1:


What am i doing wrong?

_Data["messages"]

is returning type object. You need to cast it to List<string> or IList<string> in order to use an indexer.

var indexable = _Data["messages"] as IList<string>; // The image is cut off - not sure if this should be string or not
if (indexable != null)
    return indexable[0];


来源:https://stackoverflow.com/questions/46683441/access-violation-cannot-apply-indexing-with-to-an-expression-of-type-object

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