2

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 { }
heltonbiker
  • 26,657
  • 28
  • 137
  • 252
  • So when you create an instance of the base class, you want an instance of each subtype created as well? – Drew Kennedy Aug 03 '16 at 20:00
  • @DrewKennedy not quite. When I create an instance of a client class (`Selection`), I want to create instances that derive from an abstract service class (`Calculation`) so that I can perform different types of `Calculation` on the newly created `Selection`. – heltonbiker Aug 03 '16 at 20:04
  • There is no "static inforcement" because you build the list of types yourself. Did you try using reflection to select all types that derive from the base class? – maxbeaudoin Aug 03 '16 at 20:07
  • @maxbeaudoin I didn't, I'm afraid I don't know how to do it. Could you post that as an answer? – heltonbiker Aug 03 '16 at 20:10
  • 1
    Maybe check out [this](http://stackoverflow.com/a/6944605/4204026) answer. It would be nice if Microsoft created something so this didn't seem so hacky. I can see a lot of advantages to this being in the standard .NET library. – Drew Kennedy Aug 03 '16 at 20:13
  • 1. It would be better to use interfaces instead of abstract classes when you are working with instances. 2. Like you mentioned earlier an IoC or DI framework would fit best here like Castle or NIjnect or Autofac instead of trying to use reflection to get all types although that is also doable you would be reinventing the wheel. – Igor Aug 03 '16 at 20:14
  • Linked (as duplicate http://stackoverflow.com/questions/857705/get-all-derived-types-of-a-type) post answers how to get all types you are looking for (which seem to be only missing part of your code). If you need DI-based answer consider asking separate question (also you may want to decide on DI framework yourself first as all have different ways to register multiple types) – Alexei Levenkov Aug 03 '16 at 20:18
  • @heltonbiker it seems I won't have to but reflection is what you're looking for. Drew Kennedy's comment should help. – maxbeaudoin Aug 03 '16 at 20:23

0 Answers0