违反访问权限:无法将[]的索引应用于类型为'object'的表达式 [英] Access Violation: Cannot apply indexing with [] to an expression of type 'object'

查看:50
本文介绍了违反访问权限:无法将[]的索引应用于类型为'object'的表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下变量:_Data.

I have the following variable: _Data.

该变量包含以下信息: _数据变量

The variable contains the following info: _Data Variable

我如何访问消息字段?

我尝试了_Data["messages"][0]-但它不能正常工作.

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

我收到以下错误:无法将[]的索引应用于'object'类型的表达式

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

我在做什么错了?

谢谢.

推荐答案

我在做什么错了?

What am i doing wrong?

_Data["messages"]

返回类型object.您需要将其强制转换为List<string>IList<string>才能使用索引器.

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

这篇关于违反访问权限:无法将[]的索引应用于类型为'object'的表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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