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>的更多相关文章

  1. 无法将类型“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 ...

  2. using 语句中使用的类型必须可隐式转换为“System.IDisposable

    在使用 EF 出现 using 语句中使用的类型必须可隐式转换为“System.IDisposable 今天写在这里分享给大家 出现这样的问题,是因为没有引用  EntityFramework 这个程 ...

  3. using 语句中使用的类型必须可隐式转换为“System.IDisposable”

    在entity framework 中错误 using 语句中使用的类型必须可隐式转换为“System.IDisposable” 的错误. 原因是: 没有引用 EntityFramework 这个程序 ...

  4. 【.NET】using 语句中使用的类型必须可隐式转换为"System.IDisposable"

    #问题: 在使用EF开发中,出现如下错误:“using 语句中使用的类型必须可隐式转换为“System.IDisposable” #原因: 项目中没有引用 EntityFramework 这个程序集: ...

  5. 报错:无法将类型"System.Data.EntityState"隐式转换为"System.Data.Entity.EntityState"

    报错:无法将类型"System.Data.EntityState"隐式转换为"System.Data.Entity.EntityState".   出错语句停留 ...

  6. 错误 128 无法将类型“string”隐式转换为“System.Windows.Forms.DataGridViewTextBoxColumn”

    原因是DataGridView中列的Name属性值和DataPropertyName属性值一样,比如Name="CardID",DataPropertyName="Car ...

  7. MVC 无法将类型“System.Collections.Generic.List<AnonymousType#1>”隐式转换为“System.Collections.Generic.IList<Mvc3Modeltest.Models.Movie>”。存在一个显式转换(是否缺少强制转换?))

    1.问题: 2.解决方案:强制指定类型. 解决之.

  8. 无法将类型“System.Collections.Generic.IEnumerable<EmailSystem.Model.TemplateInfo>”隐式转换为“System.Collections.Generic.List<EmailSystem.Model.TemplateInf

    List<Model.Template> templateList = templateBLL.RecommendTemplateByOrder(modelEbay); List<M ...

  9. 使用EF6.0出现:CS0029 无法将类型“System.Data.Entity.Core.Objects.ObjectContext”隐式转换为“System.Data.Objects.ObjectContext”错误

    这是因为EF6.0重构了一些命名空间后,和VS原有的实体数据模型模板不一致了(ObjectContext context = ((IObjectContextAdapter)dataContext). ...

随机推荐

  1. 今天说一下Order by 这个常规东西~

    Order by 在我们日常的数据库开发生活中是出镜率灰常高的. order by 的作用就是用于对查询出来的结果进行排序~对啊~人家就是这么接地气~比如按发生时间啊,首字母啊之类的都是相当常见. 今 ...

  2. 烂泥:apache密码生成工具htpasswd的应用

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 1. htpasswd的作用与安装 2. htpasswd命令详解 3. htpasswd的实例 4. htpasswd的应用 一.htpasswd的作 ...

  3. Opensource开源精神

    现在如火如荼的开源运动和互联网自由开放的精神是一致的,互联网上有无数非常优秀的像Linux一样的开源代码,我们千万不要高估自己写的代码真的有非常大的“商业价值”.那些大公司的代码不愿意开放的更重要的原 ...

  4. Type mismatch: cannot convert from MainFragment to Fragment 报错

    源码: FragmentTransaction mFragmentTranscation = getSupportFragmentManager().beginTransaction(); Fragm ...

  5. ES6函数默认参数(Default Parameters)

    语言更新时每一个新增的特性都是从千百万开发者需求里提取过来的,规范采用后能减少程序员的痛苦,带来便捷. 我们经常会这么写 function calc(x, y) { x = x || 0; y = y ...

  6. stm32 USART rs485 rs232

    转载自:http://www.cnblogs.com/chineseboy/archive/2013/03/06/2947173.html 前题: 前段时间,在公司调试了一个项目,很简单,但对于初学的 ...

  7. ELK日志解决方案安装配置与使用

    官方网站:https://www.elastic.co/products/elasticsearch logstash,elasticsearch,kibana作用如下: logstash:分布在每一 ...

  8. 动手学习TCP:总结和索引

    TCP是一个十分复杂的协议,通过前面几篇文章只涉及了TCP协议中一些基本的概念. 虽然说都是一些TCP最基本的概念,但是试验过程中一直在踩坑,例如:TCP flag设置错误,seq.ack号没有计算正 ...

  9. (转)linux下vi命令大全

    http://www.cnblogs.com/88999660/articles/1581524.html 进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi +n ...

  10. 单调队列优化DP,多重背包

    单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...