re: What You Need To Know About The Helpful Strategy Pattern VIEW POST


Nice article.

Apologies if this is obvious, but I like to use reflection to find classes implementing ITextExtractor followed by Activator.CreateInstance to instantiate them and add them to the array at runtime, thereby reducing the steps needed in adding a new implementation to just step 1 in your list above - "Add the new text extraction class".

Of course, this is only useful if the order of the items in the array isn't important!


James that is a slick idea. I would like to see a demo of that with some code so that I can wrap my head around it further. Thank you for the comments.


Sure! Might not be suitable for a group project, it makes assumptions that all ITextExtractor classes have parameterless constructors which isn't immediately obvious and wouldn't go down well in my employers code reviews, but for personal projects I find it useful! Something along the lines of this:

private ITextExtractor[] _extractors;

public RunExtraction()
    _extractors =  AppDomain.CurrentDomain.GetAssemblies()
        .SelectMany(x => x.GetTypes())
        .Where(t => typeof(ITextExtractor).IsAssignableFrom(t) && !t.IsInterface)
        .Select(t => (ITextExtractor)Activator.CreateInstance(t))
code of conduct - report abuse