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. IE6-9不支持Textarea的maxlength属性

    给textarea标签添加一个maxlength=200,测试工程师提bug说IE9没起作用.后一测试,发现IE10一下的浏览器均不支持 <textarea maxlength="20 ...

  2. Android开发中的Json字符串与复杂的嵌套对象互转。

    Gson 可能是大家都觉得比较简单吧.我发现用JSONObject和网上下载的JSONHelper类使用起来很无语,只能解析简单的单层对象,如果有嵌套的就不能直转转成可用对象了.所以网上找了一会儿,发 ...

  3. win7下Qt5使用mysql C++编程配置

    先下载mysql的库文件链接:http://files.cnblogs.com/files/xiaobo-Linux/mysql.zip 把两个文件放入 Qt目录\Qt5.5.0\5.5\mingw4 ...

  4. JavaScript实现简单的双向绑定

    很多的前端框架都支持数据双向绑定了,最近正好在看双向绑定的实现,就用Javascript写了几个简单的例子. 几个例子中尝试使用了下面的方式实现双向绑定: 发布/订阅模式 属性劫持 脏数据检测 发布/ ...

  5. Andorid 6连接Libreswan L2TP VPN

    手机升级到Android 6以后,以前正常使用的L2TP VPN却无法连接了.服务器端日志: "vpnpsk"[119] 114.249.245.192 #240: no acce ...

  6. opencv直线检测在c#、Android和ios下的实现方法

    opencv直线检测在c#.Android和ios下的实现方法 本文为作者原创,未经允许,不得转载 :原文由作者发表在博客园:http://www.cnblogs.com/panxiaochun/p/ ...

  7. CF721C. Journey[DP DAG]

    C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard input ou ...

  8. MarshalAs属性指示如何在托管代码和非托管代码之间封送数据。

    http://blog.csdn.net/tianyu0910/article/details/6260755 http://blog.sina.com.cn/s/blog_4e4ee8ed0100e ...

  9. QFontMetrics的一个问题

    今天发现QFontMetric的一个问题 QFontMetrics可以输入一个字体和一个字符串,计算出字符串在这种字体下的显示尺寸. 这种计算比较耗时间,因此Qt内部应该是缓存了计算结果.具体方法是在 ...

  10. JavaScript Array 对象

    JavaScript Array 对象 Array 对象 Array 对象用于在变量中存储多个值: var cars = ["Saab", "Volvo", & ...