public abstract Column<T>
{
private T Value {get;set;}
public abstract string Format();
}
public class DateColumn : Column<DateTime>
{
public override string Format()
{
return Value.ToString("dd-MMM-yyyy");
}
}
public class NumberColumn : Column<decimal>
{
public override string Format()
{
return Value.ToString();
}
}
The problem I have is adding these into a generic collection. I know its possible but how can I store multiple types in a collection etc
IList<Column<?>> columns = new List<Column<?>()
I would really appreciate any advice on achieving this goal. The goal being having different column types stored in the same List. Its worth mentioning I am using NHibernate and the discriminator to load the appropriate object.Ultimately the Value needs to have the type of the class.
Many thanks for your help in advance.
In order to be stored in a
List<T>together the columns must have a common base type. The closest common base class ofDateColumnandNumberColumnisobject. Neither derives fromColumn<T>but instead a specific and different instantiation ofColumn<T>.One solution here is to introduce a non-generic
Columntype whichColumn<T>derives from and store that in theList