0

I have a class

public class BaseHeaderFooterItem
{
  public string Title { get; set; }
  public string EnTitle { get; set; }
  public HyperLink Link { get; set; }
  public int Level { get; set; }
  public HyperLink MobileLink { get; set; }
}

Many other class inherit from him I want to have on generic list in the class BaseHeaderFooterItem that will be able to hold a list from any type of the inherited classes. something like this:

public class BaseHeaderFooterItem
    {
      public string Title { get; set; }
      public string EnTitle { get; set; }
      public HyperLink Link { get; set; }
      public int Level { get; set; }
      public HyperLink MobileLink { get; set; }
      public List<T> Descendants { get; set; }
    }

How can I do it ?

5
  • List<BaseHeaderFooterItem> will do. Commented Sep 24, 2015 at 11:49
  • @haim770 it wont. for example if I have an inherited class from BaseHeaderFooterItem, named TvGuid and want the Descendants to hold a class of TvGuid, i wont work Commented Sep 24, 2015 at 11:53
  • @AdamB it should. Why wouldn't it? Commented Sep 24, 2015 at 11:54
  • @haim770 it wont. for example if I have an inherited class from BaseHeaderFooterItem, named TvGuid and want the Descendants to hold a class of TvGuid, i wont wor Commented Sep 24, 2015 at 11:55
  • If you have a derived class exactly what type should the list hold for it. Any BaseHeaderFooterItem or only items of that derived class? Commented Sep 24, 2015 at 11:56

2 Answers 2

2

You could try to keep a properties to hold a child collection of the base type.

public class BaseHeaderFooterItem
{
  public string Title { get; set; }
  public string EnTitle { get; set; }
  public HyperLink Link { get; set; }
  public int Level { get; set; }
  public HyperLink MobileLink { get; set; }

  // here you can add instances of BaseHeaderFooterItem and any inherits type
  public List<BaseHeaderFooterItem> Descendants { get; set; } 
}

And you could add any tpe that inherits from BaseHeaderFooterItem, for sample:

var list = new List<BaseHeaderFooterItem>();

list.Add(new BaseHeaderFooterItem() {
        Title = "Test"
        Descendants = new List<BaseHeaderFooterItem>()
                                 {
                                    new ChildHeaderFooterItem() { /* properties */}
                                 }
    });

Or if you need a specif type for each BaseHeaderFooterItem, than try to specif the generic on the declaration.

public class BaseHeaderFooterItem<T>
             where T : BaseHeaderFooterItem<T>
{
  public string Title { get; set; }
  public string EnTitle { get; set; }
  public HyperLink Link { get; set; }
  public int Level { get; set; }
  public HyperLink MobileLink { get; set; }

  // only T types
  public List<T> Descendants { get; set; }
}

var list = new List<BaseHeaderFooterItem<ChildType>>();

list.Add(new BaseHeaderFooterItem() {
        Title = "Test"
        Descendants = new List<ChildType>()
                                 {
                                    new ChildHeaderFooterItem() { /* properties */}
                                 }
    });
Sign up to request clarification or add additional context in comments.

1 Comment

Maybe with a where T : BaseHeaderFooterItem<T> constraint?
0

You may use this pattern

public interface IBaseHeaderFooterItem
{
    string Title { get; set; }
    string EnTitle { get; set; }
    public HyperLink Link { get; set; }
    int Level { get; set; }
    HyperLink MobileLink { get; set; }
}

public abstract class BaseHeaderFooterItem<T> : IBaseHeaderFooterItem 
                                                where T : IBaseHeaderFooterItem
{
    public List<T> Descendants { get; set; }

     public abstract string Title { get; set; }
     public abstract string EnTitle { get; set; }
     public abstract HyperLink Link { get; set; }
     public abstract int Level { get; set; }
     public abstract HyperLink MobileLink { get; set; }
}

Then you can inherit from BaseHeaderFooterItem<T> and T is constraint to be a class implementing IBaseHeaderFooterItem and having all those properties.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.