NHibernate无法将类型“System.Collections.Generic.IList<T>”隐式转换为“System.Collections.Generic.IList<IT>
API有一个需要实现的抽象方法:
public IList<IPermission> GetPermissions();
需要注意的是IList<IPermission>这个泛型集合的类型参数IPermission是个接口。
现在我要在实现类中使用NHibernate去实现这个方法,一开始我觉得很简单。
因为有一个实体类Permission实现了IPermission接口,于是很直接的写法:
return NHibernateSession.CreateCriteria(typeof(Permission)).List<Permission>();
编译这段代码,错误:
无法将类型“System.Collections.Generic.IList<Permission>”隐式转换为“System.Collections.Generic.IList<IPermission>”。存在一个显式转换(是否缺少强制转换?)
既然说缺少强制转换,OK。我就将它强制转换一下。
return NHibernateSession.CreateCriteria(typeof(Permission)).List<Permission>() as IList<IPermission>;
这回编译通过, 于是调试,不幸的是,返回结果是null。
解决:
笨方法:
IList<Permission> permissions = NHibernateSession.CreateCriteria(typeof(Permission)).List<Permission>(); IList<IPermission> ipermissions = new IList<IPermission>(); foreach ( Permission permission in permissions )
{
ipermissions.Add(permission);
}
return ipermissions;
保留参考:
.
ipermissions.AddRange((IPermission[])permissions.ToArray()); .
ipermissions.AddRange(permissions.Cast<IPermission>()); .
Converter<Permission, IPermission> converter = delegate(Permission permission) { return permission as IPermission; };
List<IPermission> iPermissions = permissions.ConvertAll<IPermission>(converter);
NHibernate无法将类型“System.Collections.Generic.IList<T>”隐式转换为“System.Collections.Generic.IList<IT>的更多相关文章
- 无法将类型“System.Collections.Generic.List<anonymous type:string ClassID,string ClsssName>”隐式转换为“System.Collections.Generic.List<Ecology.Model.EnergyFlowGraph>”
无法将类型“System.Collections.Generic.List<anonymous type:string ClassID,string ClsssName>”隐式转换为“Sy ...
- using 语句中使用的类型必须可隐式转换为“System.IDisposable
在使用 EF 出现 using 语句中使用的类型必须可隐式转换为“System.IDisposable 今天写在这里分享给大家 出现这样的问题,是因为没有引用 EntityFramework 这个程 ...
- using 语句中使用的类型必须可隐式转换为“System.IDisposable”
在entity framework 中错误 using 语句中使用的类型必须可隐式转换为“System.IDisposable” 的错误. 原因是: 没有引用 EntityFramework 这个程序 ...
- 【.NET】using 语句中使用的类型必须可隐式转换为"System.IDisposable"
#问题: 在使用EF开发中,出现如下错误:“using 语句中使用的类型必须可隐式转换为“System.IDisposable” #原因: 项目中没有引用 EntityFramework 这个程序集: ...
- 报错:无法将类型"System.Data.EntityState"隐式转换为"System.Data.Entity.EntityState"
报错:无法将类型"System.Data.EntityState"隐式转换为"System.Data.Entity.EntityState". 出错语句停留 ...
- 错误 128 无法将类型“string”隐式转换为“System.Windows.Forms.DataGridViewTextBoxColumn”
原因是DataGridView中列的Name属性值和DataPropertyName属性值一样,比如Name="CardID",DataPropertyName="Car ...
- MVC 无法将类型“System.Collections.Generic.List<AnonymousType#1>”隐式转换为“System.Collections.Generic.IList<Mvc3Modeltest.Models.Movie>”。存在一个显式转换(是否缺少强制转换?))
1.问题: 2.解决方案:强制指定类型. 解决之.
- 无法将类型“System.Collections.Generic.IEnumerable<EmailSystem.Model.TemplateInfo>”隐式转换为“System.Collections.Generic.List<EmailSystem.Model.TemplateInf
List<Model.Template> templateList = templateBLL.RecommendTemplateByOrder(modelEbay); List<M ...
- 使用EF6.0出现:CS0029 无法将类型“System.Data.Entity.Core.Objects.ObjectContext”隐式转换为“System.Data.Objects.ObjectContext”错误
这是因为EF6.0重构了一些命名空间后,和VS原有的实体数据模型模板不一致了(ObjectContext context = ((IObjectContextAdapter)dataContext). ...
随机推荐
- MVC Ajax Helper或jQuery异步方式加载部分视图
Model: namespace MvcApplication1.Models { public class Team { public string Preletter { get; set; } ...
- 注解学习(模仿springMvc的注解注入方式)
最近在看springMvc的源码,看到了该框架的注入注解的部分觉的有点吃力,可能还是对注解的方面的知识还认识的不够深刻,所以特意去学习注解方面的知识.由于本人也是抱着学习的态度来阅读源码,若文章在表述 ...
- java学习之 反射
以前学习java只是学习了基本语法操作,各种常用方法的使用,随着慢慢学习,很多大神都觉得要想成为大神,就必须把java的反射给理解透,这样我就带着好奇的心去学习到底反射是什么玩意,所以就上网找资料学习 ...
- HDU 4630 No Pain No Game 树状数组+离线操作
题意:给一串数字,每次查询[L,R]中两个数的gcd的最大值. 解法:容易知道,要使取两个数让gcd最大,这两个数最好是倍数关系,所以处理出每个数的所有倍数,两两间根据倍数关系形成一条线段,值为该数. ...
- [No00005D]如何高效利用GitHub
原文地址:http://www.yangzhiping.com/tech/github.html 正是Github,让社会化编程成为现实.本文尝试谈谈GitHub的文化.技巧与影响. Q1:GitHu ...
- 技术专题—Python黑客【优质内容聚合贴】
作者:坏蛋链接:https://zhuanlan.zhihu.com/p/24645819来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 一.前言 本着知识分享,聚合优 ...
- HTML5网站如何做到完全不需要jQuery
jQuery是现在最流行的JavaScript工具库. 据统计,目前全世界57.3%的网站使用它.也就是说,10个网站里面,有6个使用jQuery.如果只考察使用工具库的网站,这个比例就会上升到惊人的 ...
- HTML 学习笔记 CSS(列表)
CSS列表属性允许你放置 改变列表项标志 或者将图像作为列表项标志. CSS列表 从某中意义上讲 不是描述性的文本的任何内容都可以认为是列表.人口普查.太阳系.家谱.参观菜单,甚至你的所有朋友都可以表 ...
- childNodes的兼容性问题
元素.childNodes:只读 属性 子节点列表集合 标准浏览器下:包含文本和元素类型节点,也会包含非法嵌套的子节点 非标准浏览器下:只包含元素类型节点,ie7下不会包含非法嵌套的子节点 child ...
- tomcat安装配置.md
tomcat 安装 安装jdk jdk我使用的是oracle的,如果下载请在oracle的官网上下载.或者你也可以使用openjdk,官网在http://openjdk.java.net/. # ta ...