2

I try to get all classes implementing an interface with this method:

private static IEnumerable<Type> GetDriverClasses()
{
    var type = typeof(IDeviceDriver);
    var types = AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(s => s.GetTypes())
                .Where(p => type.IsAssignableFrom(p) && p.IsClass && !p.IsAbstract);
    return types;
}

This works as long as an instance of the class has been created. else it fails.
How to get a class without having to create an instance first?

Additional info:

DllSetup:
Core.dll holds the class with the above method
Impl.dll references Core and holds the class to find
Test.dll references both and calls the method

It seems connected to how assemblies are loaded as creating an instance of a dummy class in Impl.dll also makes the other class find-able.

Dawnkeeper
  • 2,844
  • 1
  • 25
  • 41
  • 1
    Use [`AppDomain.Load()`](https://msdn.microsoft.com/en-us/library/36az8x58(v=vs.110).aspx) to force an assembly to be loaded into the app domain –  Jun 23 '17 at 14:00
  • It does not have anything to do with instances of classes but if the library (.dll) has been loaded yet into the application domain. – Igor Jun 23 '17 at 14:00
  • 1
    Does https://stackoverflow.com/a/11994178/34092 help? – mjwills Jun 23 '17 at 14:00
  • Possible duplicate of [Get type in referenced assembly by supplying class name as string?](https://stackoverflow.com/questions/11994057/get-type-in-referenced-assembly-by-supplying-class-name-as-string) – Igor Jun 23 '17 at 14:01
  • 1
    Alternative https://stackoverflow.com/questions/2384592 – Green_Wizard Jun 23 '17 at 14:03
  • Yes, its definitely the assembly not being loaded. So I will have to go the route of having a driver directory and loading all assemblies in there. – Dawnkeeper Jun 23 '17 at 14:19

1 Answers1

3

AppDomain.CurrentDomain.GetAssemblies() will contain only the assemblies already loaded in the AppDomain - assemblies get loaded once a type of the assembly is used. You need to Load all assemblies yourself using Assembly.LoadFrom(..).

brijber
  • 711
  • 5
  • 17