Include all assemblies in path

Dec 3, 2013 at 2:25 PM
I suggest adding a new method that adds all assemblies in a path.

I have a bunch of assemblies that are not loaded at start up (WPF app). It would be convenient to have a convention where it would try to load all assemblies in a path.
I have the start of an implementation that works for my needs thus far, but could (should) be expanded upon for more general use.

e.g.

Bootstrapper.Including.FromPath(...)
or my use
Bootstrapper.Including.FromPathContainingAssembly(...)
Dec 4, 2013 at 3:15 PM
I added this functionality through an extension method. Not sure why I didn't think of this yesterday.
    public static class BootstrapperExtensions
    {
        public static IIncludedAssemblies FromPathContainingAssembly(this IIncludedAssemblies bootstrapper, Assembly fromAssembly)
        {
            var uri = new UriBuilder(fromAssembly.CodeBase);
            var path = Uri.UnescapeDataString(uri.Path);
            var realPath = Path.GetDirectoryName(path);

            var assemblyPaths = Directory.GetFiles(realPath).Where(IsAssemblyFile);
            var assemblies = new Collection<Assembly>();
            foreach (var assemblyPath in assemblyPaths)
            {
                try
                {
                    var assembly = Assembly.LoadFrom(assemblyPath);
                    if (!bootstrapper.Excluding.Assemblies.Any(excluded => assembly.FullName.StartsWith(excluded)))
                    {
                        assemblies.Add(assembly);
                    }
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.Message);
                }
            }

            return bootstrapper.AssemblyRange(assemblies);
        }

        private static bool IsAssemblyFile(string fileName)
        {
            var extension = Path.GetExtension(fileName);
            return HasAssemblyExtension(extension);
        }

        private static bool HasAssemblyExtension(string extension)
        {
            const StringComparison comparison = StringComparison.OrdinalIgnoreCase;
            return string.Equals(extension, ".dll", comparison) ||
                   string.Equals(extension, ".exe", comparison);
        }
    }
Coordinator
Dec 4, 2013 at 3:24 PM
Very nice. I'll be sure to add it to next release and of course I'll give you credit for it.
Thanks ekgurnery14
Dec 4, 2013 at 3:34 PM
I got the idea from something I had done previously using Ninject, so most of the credit goes there.

Eric