簡體   English   中英

如何將對象轉換為集合或列表

[英]how to convert object to collection or list

我將linq list()/ collection結果保存為HttpContext.Current.Cache作為對象,當HttpContext.Current.Cache.Get("data")不為空時,當我嘗試將對象作為collection時,它為null:

ICollection<ActionEntity> actions = HttpContext.Current.Cache.Get("data") as ICollection<ActionEntity>;

嘗試打印數據類型。 就像是:

System.Diagnostics.Debug.WriteLine(HttpContext.Current.Cache‌​.Get("data").GetType‌​().ToString()).

然后使用該數據類型進行轉換。

您可以在Windows輸出->調試中查看結果。

編輯:

您不能強制轉換AnonymousType。 您可以做的是創建一個具有AnonymousType相同結構的對象,並改為使用它。 例如,您有一個

new {  List = new List<ActionEnti‌​ty>() }

您可以像這樣創建一個ListOfList<T>類:

class ListOfList<T> { public List<T> List { get; set; } }

而不是使用AnonymousType,請使用:

new ListOfList<ActionEntity> {  List = new List<ActionEnti‌​ty>() }

然后,您可以使用以下方法進行投射:

ListOfList<ActionEntity> data = HttpContext.Current.Cache.Get("data") as ListOfList<ActionEntity>;

暫無
暫無

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM