簡體   English   中英

如何處理類型“ Object {System.Collections.Generic.List <object> }”

[英]How to handle type “Object {System.Collections.Generic.List<object>}”

這是我第一次遇到這樣的物體。

>調試期間我的本地窗口的圖像鏈接<

簡而言之,當“測試”給出的唯一方法與普通對象完全相同,並且我不知道將其強制轉換為什么時,如何訪問CardNO或ItemID的值(分別為296和130)。 我什至無法執行此“ 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的實例。

您需要將List<Object> List<CommsItem>List<CommsItem>

例:

var test = DragDropPayloadManager.GetDataFromObject(e.Data, typeof(CommsItem)).Cast<CommsItem>().ToList();

或強制轉換每個單獨的元素:

CommsItem element = (test[0] as CommsItem);

它將返回CommsItem轉換為CommsItemelement ,除非它不是該類型或不屬於該類型,在這種情況下,它將返回null。

因此,要回答您的問題,您可以通過以下方式訪問它們:

string CardNO = (test[0] as CommsItem).CardNO;

要么

var test = DragDropPayloadManager.GetDataFromObject(e.Data, typeof(CommsItem)).Cast<CommsItem>().ToList();
string CardNO = test[0].CardNO;

(如果使用第一種方法,則不需要強制轉換)

您正在執行的操作是將List<object>test轉換為List<CommsItem> ,您可以使用它訪問屬性,或簡單地轉換每個項目。

更簡單地說:

var item = ((List<object>)test).Cast<CommsItem>().FirstOrDefault();

如果您的列表不是CommsItem列表, CommsItem返回一個空列表。 FirstOrDefault()確保僅獲取第一項,如果沒有第一項,則該項的默認值(對於引用對象,此null )。

無法隱式轉換類型 'System.Collections.Generic.List<object> ' 到 'System.Collections.Generic.IEnumerable<int><div id="text_translate"><p> 我想構建一個 function,它接受一個非負整數和字符串的列表,並返回一個過濾掉字符串的新列表。</p><p> 與此類似:</p><pre> ListFilterer.GetIntegersFromList(new List<object>(){1, 2, "a", "b"}) => {1, 2} ListFilterer.GetIntegersFromList(new List<object>(){1, 2, "a", "b", 0, 15}) => {1, 2, 0, 15}</pre><p> 為此,我執行了以下操作:(評論顯示了最初的嘗試)</p><pre> using System.Collections; using System.Collections.Generic; using System.Linq; public class ListFilterer { public static IEnumerable<int> GetIntegersFromList(List<object> listOfItems) { foreach (var item in listOfItems) { if(item is string) { listOfItems.Remove(item); } } // return listOfItems; <- Original attempt return listOfItems.ToList(); } }</pre><p> 這給出了標題中的錯誤:</p><pre> src/Solution.cs(16,13): error CS0266: Cannot implicitly convert type 'System.Collections.Generic.List<object>' to 'System.Collections.Generic.IEnumerable<int>'. An explicit conversion exists (are you missing a cast?)</pre><p> 所以我添加了我認為是正確的轉換.ToList()但它最終沒有做任何事情並且仍然提供相同的錯誤。 我對此感到困惑,因為在搜索和查看我認為類似的問題后,我仍然沒有找到正確的方法來轉換它,並且由於我對 Linq 和 Enumerable 的經驗不足,我不確定去哪里找。</p><p> 任何幫助將不勝感激,謝謝你的時間</p></div></int></object>

[英]Cannot implicitly convert type 'System.Collections.Generic.List<object>' to 'System.Collections.Generic.IEnumerable<int>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 鍵入“對象{System.Collections.Generic.List <T> }“ VS” System.Collections.Generic.List <T> ” 無法將類型&#39;object [] []&#39;轉換為&#39;System.Collections.Generic.List <object> “ 無法將類型為“System.Collections.Generic.List`1[System.Object]”的 object 轉換為類型“System.Collections.Generic.List`1” 無法將JSON對象反序列化為“System.Collections.Generic.List”類型 無法轉換&#39;System.Collections.Generic.List`1類型的對象 無法將“System.Collections.Generic.List”類型的 object 轉換為“System.Collections.Generic.ICollection”類型 無法隱式轉換類型 'System.Collections.Generic.List<object> ' 到 'System.Collections.Generic.IEnumerable<int><div id="text_translate"><p> 我想構建一個 function,它接受一個非負整數和字符串的列表,並返回一個過濾掉字符串的新列表。</p><p> 與此類似:</p><pre> ListFilterer.GetIntegersFromList(new List<object>(){1, 2, "a", "b"}) => {1, 2} ListFilterer.GetIntegersFromList(new List<object>(){1, 2, "a", "b", 0, 15}) => {1, 2, 0, 15}</pre><p> 為此,我執行了以下操作:(評論顯示了最初的嘗試)</p><pre> using System.Collections; using System.Collections.Generic; using System.Linq; public class ListFilterer { public static IEnumerable<int> GetIntegersFromList(List<object> listOfItems) { foreach (var item in listOfItems) { if(item is string) { listOfItems.Remove(item); } } // return listOfItems; <- Original attempt return listOfItems.ToList(); } }</pre><p> 這給出了標題中的錯誤:</p><pre> src/Solution.cs(16,13): error CS0266: Cannot implicitly convert type 'System.Collections.Generic.List<object>' to 'System.Collections.Generic.IEnumerable<int>'. An explicit conversion exists (are you missing a cast?)</pre><p> 所以我添加了我認為是正確的轉換.ToList()但它最終沒有做任何事情並且仍然提供相同的錯誤。 我對此感到困惑,因為在搜索和查看我認為類似的問題后,我仍然沒有找到正確的方法來轉換它,並且由於我對 Linq 和 Enumerable 的經驗不足,我不確定去哪里找。</p><p> 任何幫助將不勝感激,謝謝你的時間</p></div></int></object> 無法將 System.Collections.Generic.List[System.Object] 類型的對象轉換為 System.Collections.Generic.IList[ShortCodeList] 無法將類型為&#39;System.Collections.Generic.List`1 [System.Decimal]&#39;的對象強制轉換為&#39;System.IConvertible&#39; 如何解決此問題:“無法將“System.Collections.Generic.List`1[System.DateTime]”類型的 object 轉換為“System.IConvertible”類型。”
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM