G
Guest
I have come across a problem using generics. I want to create a Factory type
that will build classes for me. Code snippet below...
static public class Product<T> where T : new()
{
static public T New()
{
return new T();
}
}
public class Factory<T> : List<Product<T>>
{
public Factory()
{
}
public List<T> BuildAll()
{
List<T> result = new List<T>();
foreach (T item in this)
{
result.Add(Product<T>.New());
}
return result;
}
}
The problem lies in that the Factory's type <T> requires the new constraint
(like Factory), but that will conflict with the base class descriptor.
I know that I can imbed the List<> (ala Decorator pattern), but I would hope
there is another way to solve it.
that will build classes for me. Code snippet below...
static public class Product<T> where T : new()
{
static public T New()
{
return new T();
}
}
public class Factory<T> : List<Product<T>>
{
public Factory()
{
}
public List<T> BuildAll()
{
List<T> result = new List<T>();
foreach (T item in this)
{
result.Add(Product<T>.New());
}
return result;
}
}
The problem lies in that the Factory's type <T> requires the new constraint
(like Factory), but that will conflict with the base class descriptor.
I know that I can imbed the List<> (ala Decorator pattern), but I would hope
there is another way to solve it.