question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New scan method FromAssemblyInThisApplication

See original GitHub issue

Similar to the one exposed by Castle Windsor.

Castle.MicroKernel.Registration.Classes.FromAssemblyInThisApplication

/// <summary>Scans current assembly and all refernced assemblies with the same first part of the name.</summary>
/// <returns> </returns>
/// <remarks>
///     Assemblies are considered to belong to the same application based on the first part of the name. For example if the method is called from within <c>MyApp.exe</c> and <c>MyApp.exe</c> references
///     <c>MyApp.SuperFeatures.dll</c>, <c>mscorlib.dll</c> and <c>ThirdPartyCompany.UberControls.dll</c> the <c>MyApp.exe</c> and <c>MyApp.SuperFeatures.dll</c> will be scanned for components, and other
///     assemblies will be ignored.
/// </remarks>
[MethodImpl(MethodImplOptions.NoInlining)]
public static FromAssemblyDescriptor FromAssemblyInThisApplication()
{
	var assemblies = new HashSet<Assembly>(ReflectionUtil.GetApplicationAssemblies(Assembly.GetCallingAssembly()));
	return new FromAssemblyDescriptor(assemblies, Filter);
}

This is the extension I implemented over Scrutor, you can integrate it in the project if you think it worth (based on castle source code!):

public static class ScrutorAssemblySelectorExtensions
{
    public static IImplementationTypeSelector FromAssemblyInThisApplication(this IAssemblySelector assemblySelector)
    {
        var assemblies = ReflectionUtils.GetApplicationAssemblies(Assembly.GetCallingAssembly());
        var typeSelector = assemblySelector.FromAssemblies(assemblies);
        return typeSelector;
    }
}

public static class ReflectionUtils
{
    public static IEnumerable<Assembly> GetApplicationAssemblies(Assembly rootAssembly)
    {
        var index = rootAssembly.FullName.IndexOfAny(new[] { '.', ',' });
        if (index < 0)
        {
            throw new ArgumentException(
                string.Format("Could not determine application name for assembly \"{0}\". Please use a different method for obtaining assemblies.",
                                rootAssembly.FullName));
        }

        var applicationName = rootAssembly.FullName.Substring(0, index);
        var assemblies = new HashSet<Assembly>();
        AddApplicationAssemblies(rootAssembly, assemblies, applicationName);
        return assemblies;
    }

    private static void AddApplicationAssemblies(Assembly assembly, HashSet<Assembly> assemblies, string applicationName)
    {
        if (assemblies.Add(assembly) == false)
        {
            return;
        }
        foreach (var referencedAssembly in assembly.GetReferencedAssemblies())
        {
            if (IsApplicationAssembly(applicationName, referencedAssembly.FullName))
            {
                AddApplicationAssemblies(LoadAssembly(referencedAssembly), assemblies, applicationName);
            }
        }
    }

    private static bool IsApplicationAssembly(string applicationName, string assemblyName)
    {
        return assemblyName.StartsWith(applicationName);
    }

    private static Assembly LoadAssembly(AssemblyName assemblyName)
    {
        return Assembly.Load(assemblyName);
    }
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
khellangcommented, Jan 18, 2017

Personally I think this approach is a bit too aggressive, since it traverses the whole assembly tree and potentially loads lots of assemblies. Since it’s pretty easy to implement this yourself as an extension method, I’m inclined to let this sit for a while and see if there’s any interest in this from others 😄

0reactions
micdennycommented, Apr 28, 2017

sorry I did’t have a chance to test it, but I’m confident it will works 👅

Read more comments on GitHub >

github_iconTop Results From Across the Web

Classes.cs
... new FromAssemblyDescriptor(assemblies, Filter); } /// <summary>Scans current ... For example if the method is called from within <c>MyApp.exe</c> and ...
Read more >
factory - Castle Windsor: A better way to implement 2 levels ...
Here is the full code to handle any messages: var container = new WindsorContainer(); container.Register(Classes.FromAssemblyInThisApplication() ...
Read more >
Untitled
Create a new Simple Injector container var container = new Container (); // 2. ... is just a function which scans the assembly...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found