I have a classlib and a solution located in different directories.
from classlib I am calling GetCallingAssembly that returns an executable from solution followed by callingAssembly.GetReferencedAssemblies that returns the references, now I want to perform an optimization to filter out the references and remove all package refs, aiming to find a certain type.
Note: I don't want to depend on Name property of references.
...if you just want to find all types in all loaded assemblies that are derived from your AggregateRoot
type, without searching assemblies that can't possibly contain a subclass, then all you need is this:
public static IEnumerable<Type> GetAllTypesDerivedFromAggregateRoot()
{
Assembly[] asses = AppDomain.Current.GetAssemblies();
Type typeofRoot = typeof(AggregateRoot);
AssemblyName mustReferenceThisAssembly = typeofRoot.Assembly.GetName();
List<Assembly> candidates = asses
.Where( ass => ass
.GetReferencedAssemblies()
.Any( ra => AssemblyName.ReferenceMatches( reference: ra, definition: mustReferenceThisAssembly ) )
)
.ToList();
foreach( Assembly candidate in candidates )
{
Type[] types = candidate.GetTypes();
foreach( Type t in types )
{
if( typeofRoot.IsAssignableFrom( t ) ) yield return t;
}
}
}