【发布时间】:2011-04-28 12:04:52
【问题描述】:
我有三个班级:Post、Post<T> 和 WallPost。
Post 是一个抽象类,表示具有 name、text、user 等属性的博客文章。
第二个抽象类是Post<T>,它是post 的通用版本,所以我可以实现一个通用的GetChildren(T Post) 来获取post 的子级并构建一个层次列表。
第三类是具体的“WallPost”,它是针对用户个人资料的特定帖子。
public class WallPost : Post<WallPost>
最后是一个Member 类,其中包含WallPosts 的集合。
我编写了一个控件,它接收一组帖子并将其显示在树中。我对该控件的属性之一是
public IList<Post<Post>> Posts
{
get; set;
}
(我使用泛型是因为我需要得到孩子,GetChildren(T Post))。我将 type 参数设置为 Post,因此它将接受从 post、wallpost、blogpost 等继承的任何类
现在我的问题是我想将IList<WallPost> 类型的用户集合IList<WallPost> 传递给函数,我收到此错误:
Unable to cast object of type 'NHibernate.Collection.Generic.
PersistentGenericBag`1[BO.WallPost]'
to type 'System.Collections.Generic.IList`1[BO.Post`1[BO.Post]]'.
我猜这是因为虽然你可以写 Post p = new WallPost() 这样的东西,但你不能有 List<Post> posts = new List<WallPost>()
无论如何,我完全感到困惑,我希望我说清楚了。
提前谢谢你
E
附:我使用 NHibernate,没有提到任何包。
【问题讨论】:
-
伙计,您尝试传递的集合显然不是 IList
。看异常,是一个NHibernate.Collection.Generic.PersistentGenericBag -
Explicit Casting Problem 的可能重复项
-
我注意到,但在我的代码中没有提到包或任何东西,都是 IList
标签: c# .net asp.net nhibernate generics