DEV Community

Discussion on: What You Need To Know About The Helpful Strategy Pattern

Collapse
 
kylegalbraith profile image
Kyle Galbraith

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.

Collapse
 
risingsungames profile image
James

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))
        .ToArray();
}