为什么在向列表中添加其他对象时,此ploymorphic List属性为null? [英] Why does this ploymorphic List property go null when adding additional objects to the list?

查看:312
本文介绍了为什么在向列表中添加其他对象时,此ploymorphic List属性为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下内容:

public class VideoContainer<T>
{
    public string Name { get; set; }
    //public List<VideoContainer<T>> VideoContainers { get; set; }
}

public class Perspective : VideoContainer<Perspective>
{
    public List<VideoContainer<SourceContainer>> VideoContainers { get; set; }
}

我想确保 VideoContainer< Perspective> ;. VideoContainers 只能包含 VideoContainer< SourceContainer> 类型。

I want to ensure VideoContainer<Perspective>.VideoContainers can only contain VideoContainer<SourceContainer> types.

我向列表<透视图> 添加一个新的 Perspective $ c>与三个 VideoContainers 。问题是,当我向列表中添加一个新的Perspective时,之前添加的 Perspective.VideoContainers 为null。

I add a new Perspective object to a List<Perspective> with three VideoContainers. The problem is that when I add a new Perspective to the list, the previously-added Perspective.VideoContainers is null.

为什么会发生这种情况?

Why is this happening?

推荐答案

听起来你需要两个通用类型:

It sounds like you need two generic types:

public class VideoContainer<T, U>
{
    public string Name { get; set; }
    public List<VideoContainer<U>> VideoContainers { get; set; }
}

public class Perspective : VideoContainer<Perspective, SourceContainer>
{
    // No longer declare the list, just use it... it's now:
    // public List<VideoContainer<SourceContainer>> VideoContainers { get; set; }
}

这篇关于为什么在向列表中添加其他对象时,此ploymorphic List属性为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆