简体   繁体   English

generics 中的 inheritance 是否与具体和抽象类的工作方式相同,InvalidCaseException

[英]Does inheritance in generics work the same way as concrete and abstract classes, InvalidCaseException

I have three classes: Post , Post<T> and WallPost .我有三个类: PostPost<T>WallPost

Post is an abstract class representing a blog post with properties like name , text , user , etc. Post 是一个抽象的 class 表示具有nametextuser等属性的博客文章。

The second abstract class is Post<T> , the generic version of post so I can implement a generic GetChildren(T Post) to get the children of a post and build a hierarchal list.第二个抽象 class 是Post<T> , post 的通用版本,因此我可以实现通用GetChildren(T Post)来获取post的子级并构建层次结构列表。

The third class is the concrete ' WallPost ' which is a specific post for a user's profile.第三个 class 是具体的“ WallPost ”,它是针对用户个人资料的特定帖子。

public class WallPost : Post<WallPost>

And finally a Member class which contains a collection of WallPosts .最后是一个包含 WallPosts 集合的Member WallPosts


I wrote a control that takes a collection of posts and displays it in a tree.我编写了一个控件,它接收一组帖子并将其显示在树中。 One of the proerties I have on that control is我对该控件的属性之一是

public IList<Post<Post>> Posts
        {
            get; set;
        }

(I use generics because I need to get the children, GetChildren(T Post) ). (我使用 generics 因为我需要带孩子GetChildren(T Post) )。 and I set the type parameter as Post so it will accept any class that inherits from post, wallpost, blogpost, etc我将类型参数设置为 Post,因此它将接受任何继承自 post、wallpost、blogpost 等的 class

Now my problem is that I want to pass a user's collection of WallPosts of type IList<WallPost> to the function and I get this error:现在我的问题是我想将用户的IList<WallPost>类型的WallPosts集合传递给 function 并且我收到此错误:

     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]]'.

I'm guessing this is because although you can write something like Post p = new WallPost() you can't have List<Post> posts = new List<WallPost>()我猜这是因为虽然你可以写像Post p = new WallPost()你不能有List<Post> posts = new List<WallPost>()

Anyways I'm completely baffled and I hope I made myself clear.无论如何,我完全感到困惑,我希望我说清楚了。

Thank you in advance先感谢您

E

PS I use NHibernate and there is no mention whatsoever of a bag. PS 我使用 NHibernate 并没有提到任何包。

You're trying to use generic covariance in a way that is not type-safe.您正在尝试以非类型安全的方式使用泛型协方差。

What would happen if you write如果你写会发生什么

List<WallPost> wallList = new List<WallPost>();
List<Post> pList = wallList;
pList.Add(new OtherPost());        //Not a WallPost!

You can only do with with read-only interfaces ( IEnumerable<Post> ), or arrays.您只能使用只读接口 ( IEnumerable<Post> ) 或 arrays。

You need to check your NHibernate mappings.您需要检查您的 NHibernate 映射。 NHibernate is treating your collection as a bag instead of as a list. NHibernate 将您的收藏视为袋子而不是列表。

From the NHibernate perspective, a list may not be the appropriate choice.从 NHibernate 的角度来看,列表可能不是合适的选择。 A list gives you index-based access to the members of your collection, but you must have a list index column in your table in order for NHibernate to map it as a list.列表为您提供对集合成员的基于索引的访问,但您的表中必须有一个列表索引列,以便 NHibernate 到 map 将其作为列表。

You should probably check out the NHibernate collection mapping options and choose the one that best matches your case (set, bag, list, etc.).您可能应该查看 NHibernate 集合映射选项并选择最匹配您的情况的一个(集合、包、列表等)。

In order to work, you could use:为了工作,您可以使用:

public IList<Post<WallPost>> Posts
{
    get; set;
}

and casting when assigning it:并在分配时进行转换:

bla.Posts = posts.Cast<Post<WallPost>>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM