Last active
July 13, 2017 15:01
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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