locked
Reflection - Getting all classes that implement a given interface RRS feed

  • Question

  • I'm trying to get a list of classes that implement a certain interface but it seems that reflection is very different in WinRT and so far I couldn't find a good example of doing so.

    Does anyone know, how to load the current assembly and loop through it?

    Any help is much appreciated :)


    -Soroush

    Tuesday, November 13, 2012 5:26 PM

Answers

  • here is a simple implementation that selects all the classes that implements IDisposable... just to give you a taste of it:

    // We get the current assembly through the current class
    var currentAssembly = this.GetType().GetTypeInfo().Assembly;
    
    // we filter the defined classes according to the interfaces they implement
    var iDisposableAssemblies = currentAssembly.DefinedTypes.Where(type => type.ImplementedInterfaces.Any(inter => inter == typeof(IDisposable))).ToList();

    hope it helps...

    Can Bilgin
    Blog CompuSight

    • Proposed as answer by Dave SmitsMVP Tuesday, November 13, 2012 7:41 PM
    • Marked as answer by Soroush Mirzaei Tuesday, November 13, 2012 8:49 PM
    Tuesday, November 13, 2012 7:38 PM

All replies

  • here is a simple implementation that selects all the classes that implements IDisposable... just to give you a taste of it:

    // We get the current assembly through the current class
    var currentAssembly = this.GetType().GetTypeInfo().Assembly;
    
    // we filter the defined classes according to the interfaces they implement
    var iDisposableAssemblies = currentAssembly.DefinedTypes.Where(type => type.ImplementedInterfaces.Any(inter => inter == typeof(IDisposable))).ToList();

    hope it helps...

    Can Bilgin
    Blog CompuSight

    • Proposed as answer by Dave SmitsMVP Tuesday, November 13, 2012 7:41 PM
    • Marked as answer by Soroush Mirzaei Tuesday, November 13, 2012 8:49 PM
    Tuesday, November 13, 2012 7:38 PM
  • Thanks, exactly what I was looking for.

    -Soroush

    Tuesday, November 13, 2012 8:49 PM