loading...

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

TOP OF THREAD FULL DISCUSSION
re: 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 comme...
 

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();
}
code of conduct - report abuse