My C#/.Net application has classes Selection and Calculation. Calculation is abstract and has a handful of subclasses.
For each Selection instance, I need a collection of different possible calculations. I currently have MaxCalculation, AverageCalculation, and there will be yet unforeseen Calculation subtypes.
So, I want my application to "know" the available Calculation subtypes (possibly via dynamic dll loading and/or IoC container), and have this collection of types available, so that each time I create a new instance of Selection, I can create for it a new instance of each Calculation subtype.
I created the following spike, but it doesn't seem to be any static inforcement that the classes are subtypes of Calculation:
class Program
{
static void Main(string[] args)
{
// no static inforcement that types derive from `Calculation`
List<Type> types = new List<Type> { typeof(MaxCalculation), typeof(AverageCalculation) };
// have to cast instances to `Calculation` in order to compile
IEnumerable<Calculation> instances = types.Select(t => Activator.CreateInstance(t) as Calculation);
}
}
abstract class Calculation { }
class MaxCalculation : Calculation { }
class AverageCalculation : Calculation { }