0

I would like get a list of all classes implement a specific interface without loading the dll\s first.

Currently I use this code:

Assembly assembly = Assembly.LoadFile(dllPath);
var instances = from t in assembly.GetTypes()
                where t.GetInterfaces().Contains(interfaceType) &&
                t.GetConstructor(Type.EmptyTypes) != null
                select t.Name;
return instances.ToList();

The problem with this code is that it loads all the dependent dlls first, before getting the class names.

Jacob
  • 3,598
  • 4
  • 35
  • 56
  • 2
    Why is that a problem? – Igor Mar 30 '16 at 21:08
  • 2
    Possible duplicate of [Getting custom assembly attributes without loading into current AppDomain](http://stackoverflow.com/questions/10439668/getting-custom-assembly-attributes-without-loading-into-current-appdomain) – Jason Watkins Mar 30 '16 at 21:08
  • 7
    How would you expect it to know the classes without loading the assemblies? That's like asking for results from a database before connecting to it. – Jon Skeet Mar 30 '16 at 21:10
  • As I understand from your comment, this is the only way to get the implement classes. And the only way to unload these dlls is by using AppDomain if i'm not mistaken. This function is stays in the BL service after uploading dlls and right before saving the implemented classes in the db and I don't need them anymore so I would like to unload them or get the implementations without loading them first. @JonSkeet – Jacob Mar 30 '16 at 21:27
  • 4
    So you either need to a) use a separate AppDomain for this; b) write some alternative code to load the types in an assembly (I'd really suggest not doing that); c) just live with the assemblies being loaded. (Are there really that many?) Fundamentally you can't get at information derived from the data without loading the data in some form first though... – Jon Skeet Mar 30 '16 at 21:33
  • OK I think I'll go on the first option you gave me and use a separate AppDomain. Thanks a lot. @JonSkeet – Jacob Mar 30 '16 at 21:39
  • 1
    If you use Mono.Cecil or Microsoft CCI, then there is no need to use reflection at all and no need to use a separate app domain. – Lex Li Mar 31 '16 at 01:49

0 Answers0