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). ...
随机推荐
- 【转】input输入框中光标高度的变化问题
原文地址:http://blog.csdn.net/luochao_tj/article/details/17755457 input[type='text']文本框光标高度在有输入内容和为空时发生很 ...
- spring 注入一个以枚举类型对象
1.枚举 在实际编程中,往往存在着这样的“数据集”,它们的数值在程序中是稳定的,而且“数据集”中的元素是有限的. 例如星期一到星期日七个数据元素组成了一周的“数据集”,春夏秋冬四个数据元素组成了四季的 ...
- 关于TP3.2微信开发那点事(基础篇)
许久没有为博客更新内容,今天我将过去一周做的微信服务号的相关心得体会在此分享,具体如何申请成为服务号的相关流程文档都有,可根据要求完成: 开发第一步:开发前配置: AppID-->微信号的&qu ...
- REST架构
网络上的所有事物都被抽象为资源(resource): 每个资源对应一个唯一的资源标识符(resource identifier): 通过通用的连接器接口(generic connector inter ...
- 转: YAML 语言教程 from(阮一峰)
YAML 语言教程 from: http://www.ruanyifeng.com/blog/2016/07/yaml.html
- MVC PageList使用(异步 与 正常)
此项目的功能为1.将数据分页显示,2.搜索数据按分页显示 3.异步或同步传递 一.第一步引用 mvc PageList插件 二.控制器写法 ) //为空则默认第一页 { var lm = DataBL ...
- JavaScript 全局属性/函数
JavaScript 全局 JavaScript 全局属性和方法可用于创建Javascript对象. JavaScript 全局属性 属性 描述 Infinity 代表正的无穷大的数值. NaN 指示 ...
- 1D1D动态规划优化初步
再学习一下动态规划的基本优化方法- 首先这篇文章应该大家都看过吧-没看过的自行百度 关于实现的思路文章里都给好了-这篇就主要给一点题目啥的 (P.S. 电脑重装了,如果博客发出来有一些奇怪的问题不要在 ...
- nginx限制上传大小和超时时间设置说明/php限制上传大小
现象说明:在服务器上部署了一套后台环境,使用的是nginx反向代理tomcat架构,在后台里上传一个70M的视频文件,上传到一半就失效了! 原因是nginx配置里限制了上传文件的大小 client_m ...
- NOIP2016提高组解题报告
NOIP2016提高组解题报告 更正:NOIP day1 T2天天爱跑步 解题思路见代码. NOIP2016代码整合