0

I want to enumerate all types in large app and find all classes implementing IFoo

Using Skeet answer to similar question

var types = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
             from type in assembly.GetTypes()
             where Attribute.IsDefined(type, typeof(SerializableAttribute))
             select type).ToList();

I see how to do this but a couple of comments I saw make me doubt when I can do this.

When will the list be complete. I mean at what point in the appdomain lifecycle are all the assemblies loaded. Obviously dynamically loaded ones are not loaded immediately, but what about static ones. Is the loading deferred until first use? My original solution to the requirement involved static constructors but that suffers from the problem that these are lazily called.

pm100
  • 48,078
  • 23
  • 82
  • 145
  • Okay, other attempt at clarifying: Why do want to do that, and when do you want to do it? Because it looks like you're not controlling the main entry point, otherwise you would have done it there and then. – Frédéric Hamidi Jul 15 '14 at 23:26
  • 1
    assemblies typically load on an as needed basis, unless explicitly loaded by user code,so it will get different results depending where you call the query. – terrybozzio Jul 15 '14 at 23:27
  • Assemblies are loaded as-needed, so there's no guarantee as to which assemblies are loaded. You may consider [forcing all referenced assemblies to load first](http://stackoverflow.com/questions/2384592/c-net-is-there-a-way-to-force-all-referenced-assemblies-to-be-loaded-into-the) – Ryan Emerle Jul 15 '14 at 23:39
  • Note that `assembly.GetTypes()` can easily fail. I.e. if base types are missing/ or 32/64bit flavor specific. – Alexei Levenkov Jul 16 '14 at 00:33

0 Answers0