Class with generic list of the inherited class type

若如初见. 提交于 2019-12-11 18:03:55

问题


I have 3 class:

some of theme have list called descendents of some type.

I would like to have a generic class in the BaseHeaderFooterItem class. and insery and type of list to it.

Is there any option ?

#region ParentItem
public class BaseHeaderFooterItem
{
    public string Title { get; set; }
    public string EnTitle { get; set; }
    public HyperLink Link { get; set; }
    public int Level { get; set; }
}
#endregion

#region HeaderFooter
public class HeaderFooter : BaseHeaderFooterItem
{
    public List<Category> Descendants { get; set; }
}
#endregion

#region HeaderFooter
public class Category : BaseHeaderFooterItem
{
    public List<Show> Descendants { get; set; }
}
#endregion

#region Header
public class Show : HeaderFooter
{
    public string ImagePath { get; set; }
    public string MobileLink { get; set; }
    public string MobileLinkTarget { get; set; }
}
#endregion

#region TvGuid
public class TvGuid : Show
{
    public string Date { get; set; }
    public string Time { get; set; }
    public int IsActive { get; set; }
    public int NoProgram { get; set; }
}
#endregion

回答1:


If you want to use generics, you could use generics:

 #region ParentItem
    public class BaseHeaderFooterItem<T>
        where T:class 
    {
        public string Title { get; set; }
        public string EnTitle { get; set; }
        public HyperLink Link { get; set; }
        public int Level { get; set; }
        public List<T> Descendants { get; set; }
    }
    #endregion

    #region HeaderFooter
    public class HeaderFooter : BaseHeaderFooterItem<Category>
    {
    }
    #endregion

    #region HeaderFooter
    public class Category : BaseHeaderFooterItem<Show>
    {
    }
    #endregion

    #region Header
    public class Show : HeaderFooter
    {
        public string ImagePath { get; set; }
        public string MobileLink { get; set; }
        public string MobileLinkTarget { get; set; }
    }
    #endregion

    #region TvGuid
    public class TvGuid : Show
    {
        public string Date { get; set; }
        public string Time { get; set; }
        public int IsActive { get; set; }
        public int NoProgram { get; set; }
    }
    #endregion


来源:https://stackoverflow.com/questions/32689363/class-with-generic-list-of-the-inherited-class-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!