简体   繁体   中英

Object returned from FieldInfo.GetValue cannot be cast to Array

I'm working in a PCL (Profile7), and I've got an issue where the object returned from fieldInfo.GetValue(myItem) cannot be cast to an array.

I get a cast exception on a direct cast and (obviously) null when using As

在此处输入图片说明

how do I cast this object to a collection?

note: I have no idea what the type is as I don't have access to it. It's internal to the library I'm consuming

Check the type of the dynamicProperties object, it's not visible in the debugger screenshot that you showed. Most likely it's a List<> of some sort.

The cast you attempt only succeeds if it's an array of reference types (eg string[] or object[] , it will fail if the type is a struct, as illustrated in this question: object to string array

In any case, the IReadOnlyCollection<T> , IReadOnlyList<T> and IEnumerable<T> interfaces are covariant, you could try casting to IEnumerable<object> , or if you need more information such as count or indexer access, try one of the other interfaces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM