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

re: Nice article. Apologies if this is obvious, but I like to use reflection to find classes implementing ITextExtractor followed by Activator.CreateI...

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