如何处理类型"Object {System.Collections.Generic.List< object>}"
问题描述:
这是我第一次遇到这样的物体.
This is my first time encountering with such an object.
因此,很简单地说,当唯一的"test"给出的方法与普通对象完全相同,并且我不知道要强制转换时,如何访问CardNO或ItemID的值,分别为296和130.它来.我什至不能做这个'test [0]'.
So to put it very simply, how do I access the value for CardNO or ItemID, which is 296 and 130 respectively when the only methods 'test' give are exactly like a normal object and I don't know what to cast it to. I can't even do this 'test[0]'.
这是测试"的来源:
private void ListBoxIssue_OnDragEnter(object sender, DragEventArgs e)
{
var test = DragDropPayloadManager.GetDataFromObject(e.Data, typeof(CommsItem));
}
答
使用
var item = (CommsItem)((List<object>)test).FirstOrDefault();
在转换之前,请务必先检查 test
是否为 List< object>
的实例,以及test [0]是否为CommsItem的实例.
Be sure to check first if test
is an instance of List<object>
before casting, and if test[0] is an instance of CommsItem.