I am trying to create a generic class which new\'s up an instance of the generic type. As follows:
public class HomepageCarousel : List
Jared's answer is still a good way to go - you just need to make the constructor take the Func and stash it for later:
public class HomepageCarousel : List where T: IHomepageCarouselItem
{
private readonly Func factory;
public HomepageCarousel(Func factory)
{
this.factory = factory;
}
private List GetInitialCarouselData()
{
List carouselItems = new List();
if (jewellerHomepages != null)
{
foreach (PageData pageData in jewellerHomepages)
{
T homepageMgmtCarouselItem = factory(pageData);
carouselItems.Add(homepageMgmtCarouselItem);
}
}
return carouselItems;
}
Then you just pass the function into the constructor where you create the new instance of the HomepageCarousel.
(I'd recommend composition instead of inheritance, btw... deriving from List is almost always the wrong way to go.)