【发布时间】:2018-02-21 18:24:17
【问题描述】:
我有一个方法来返回一个类 A 的列表。但是我正在存储另一个具有相同属性的类的列表,并且需要返回这个类。
代码:
public List<Menu> GetAllMenus()
{
Menu _menu = null;
List<Menu> MenuList = new List<Menu>();
List<Menu2> CacheMenuList=new List<Menu2>();
//Caching
string CacheKey = "GetAllMenus";
ObjectCache cache = MemoryCache.Default;
if (cache.Contains(CacheKey))
CacheMenuList= (List<Menu2>)cache.Get(CacheKey);
return CacheMenuList
}
Menu 和 Menu2 类型都具有相同的属性。 由于要求,我需要将其作为 Menu2 类型的另一个列表返回。 在上面的代码中,由于它是 Menu2 类型,因此无法返回 CacheMenuList。有没有其他方法我可以做到。我收到以下错误。
无法将类型“System.Collections.Generic.List
”隐式转换为“System.Collections.Generic.List ”DailyThanthi.Repository D:\Prjct\DTNewsRevamp\ DailyThanthi.Common\DailyThanthi.Repository\MenuRepository.cs 85 活动
【问题讨论】:
-
您必须将
Menu类型的每个对象转换或转换为Menu2。查看“automapper”作为一种方式,或者只是编写代码来完成它。
标签: c# asp.net generics collections