New scan method FromAssemblyInThisApplication
See original GitHub issueSimilar 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:
- Created 7 years ago
- Reactions:2
- Comments:5 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 😄
sorry I did’t have a chance to test it, but I’m confident it will works 👅