Skip to content

Instantly share code, notes, and snippets.

@mrousavy
Last active July 13, 2017 15:01
Show Gist options
  • Save mrousavy/59e454ce424f9a8abdb2d4268f6ee4fd to your computer and use it in GitHub Desktop.
Save mrousavy/59e454ce424f9a8abdb2d4268f6ee4fd to your computer and use it in GitHub Desktop.
Get ALL references that can be found in the current Assembly, excluding own references (takes ~400ms on 34 .NET (core) references)
/// <summary>
/// Load all references/namespaces that can be found in this assembly (excluding own references)
/// </summary>
public static string[] LoadReferences()
{
//Get all namespaces from this assembly (own project, own library, ..)
IEnumerable<string> ownNamespaces = Assembly.GetExecutingAssembly().GetTypes()
.Select(t => t.Namespace)
.Distinct();
List<string> allNamespaces = new List<string>();
//Get all .NET defined types
IEnumerable<Type[]> types = AppDomain.CurrentDomain
.GetAssemblies()
.Where(a => Assembly.GetCallingAssembly().GetReferencedAssemblies().Select(ra => ra.FullName).Contains(a.FullName))
.Select(a => a.GetExportedTypes());
//Add all types where namespace is not own namespace (no own references)
foreach (Type[] type in types)
{
allNamespaces.AddRange(type
.Select(t => t.Namespace)
.Where(n => n != null && !ownNamespaces.Contains(n))
.Distinct());
}
return allNamespaces.ToArray();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment