Skip to content

Instantly share code, notes, and snippets.

@johnllao
Created November 11, 2014 05:03
Show Gist options
  • Select an option

  • Save johnllao/18d8d5019532d67d8071 to your computer and use it in GitHub Desktop.

Select an option

Save johnllao/18d8d5019532d67d8071 to your computer and use it in GitHub Desktop.
UnityAssemblyExtensions.cs
public static class UnityAssemblyExtensions
{
public static IUnityContainer RegisterTypesFromAssembliesInBasePath<T>(this IUnityContainer container)
{
var types = FromAssembliesInBasePath<T>();
foreach (var t in types)
{
container.RegisterType(typeof(T), t, t.FullName);
}
return container;
}
private static IEnumerable<Type> FromAssembliesInBasePath<T>()
{
return FromCheckedAssemblies<T>(GetAssembliesInBasePath<T>());
}
private static IEnumerable<Type> FromCheckedAssemblies<T>(IEnumerable<Assembly> assemblies)
{
return assemblies.SelectMany(a => a.DefinedTypes
.Where(ti => ti.IsClass & !ti.IsAbstract && !ti.IsValueType && ti.IsVisible && IsInheritedFromInterface<T>(ti.AsType()))
.Select(ti => ti.AsType()));
}
private static IEnumerable<Assembly> GetAssembliesInBasePath<T>()
{
return Directory.EnumerateFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll")
.Select(a => LoadAssembly(Path.GetFileNameWithoutExtension(a)))
.Where(a => a != null && !IsSystemAssembly(a) && !IsUnityAssembly(a) && ContainsType<T>(a));
}
private static Assembly LoadAssembly(string assemblyName)
{
try
{
return Assembly.Load(assemblyName);
}
catch
{
return null;
}
}
private static bool ContainsType<T>(Assembly assembly)
{
if (assembly == null)
return false;
return assembly.DefinedTypes.Select(t => t.AsType())
.Where(t => IsInheritedFromInterface<T>(t))
.ToArray()
.Length > 0;
}
private static bool IsInheritedFromInterface<T>(Type type)
{
if (type == null)
return false;
return type.FindInterfaces((m, f) => m == typeof(T), null).Length > 0;
}
private static bool IsSystemAssembly(Assembly assembly)
{
if (assembly == null)
return false;
var netFrameworkProductName = GetNetFrameworkProductName();
if (netFrameworkProductName != null)
{
var productAttribute = assembly.GetCustomAttribute<AssemblyProductAttribute>();
return productAttribute != null && string.Compare(netFrameworkProductName, productAttribute.Product, StringComparison.Ordinal) == 0;
}
else
{
return false;
}
}
private static bool IsUnityAssembly(Assembly assembly)
{
if (assembly == null)
return false;
var unityProductName = GetUnityProductName();
if (unityProductName != null)
{
var productAttribute = assembly.GetCustomAttribute<AssemblyProductAttribute>();
return productAttribute != null && string.Compare(unityProductName, productAttribute.Product, StringComparison.Ordinal) == 0;
}
else
{
return false;
}
}
private static string GetNetFrameworkProductName()
{
var productAttribute = typeof(object).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyProductAttribute>();
return productAttribute != null ? productAttribute.Product : null;
}
private static string GetUnityProductName()
{
var productAttribute = typeof(AllClasses).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyProductAttribute>();
return productAttribute != null ? productAttribute.Product : null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment