What is your actual goal? If you really want to return a list from GetData that contains AD1, you can do this:
protected virtual List<AbstractDType> GetData() { var stuff = new List<AbstractDType>(); stuff.Add( new AD1() ); return stuff; }
But if you really want to return a list of a specific type, then, well, you cannot do this - and this indicates a flaw in your design. Remember the call code, if possible:
public void Victim(DTypeStrategy strat) { List<AbstractDType> list = strat.GetData();
Ask yourself:
- Why do I want to return a custom list, and not one from AbstractDType?
- Why should it be a list at all, and not IEnumerable?
If you MUST have a custom list for any reason, use the general method:
public class DTypeStrategy<T> where T: AbstractDType { //not sure a concrete here is a good idea, but you get the point... public virtual List<T> GetData() { return new List<T>(); } } public class MyDraw : DTypeStrategy<AD2> { public override List<AD2> GetData() { return new List<AD2>(); } }
If you don't need a List<T> , you may possibly abuse C # covariant generic interfaces to do this - but then again, I look at your design first.
Philip ieck
source share