MEF | Managed Extension Framework | Get extenions’ filenames

So I read on the site that someone was having a hard time figuring out how to get a list of the addons that were being loaded as plugins. So I figured I’d look into it and add it here.

       'Create our Catalog of different Catalogs
        Dim catalog = New AggregateCatalog()

        'Add in any plugins that are in our own assembly
        catalog.Catalogs.Add(New AssemblyCatalog(GetType(Main).Assembly))
        'Check to see if our directory for our Plugins exists, if so add it as well
        If Directory.Exists("addons") Then catalog.Catalogs.Add(New DirectoryCatalog("addons", "*.dll"))
        'If we have any saved Locations outside of the main base locations (I use this in my project)
        If My.Settings.AddonLocations.Count > 0 Then
            For Each addloc In My.Settings.AddonLocations
                If System.IO.Directory.Exists(addloc.ToString) Then
                    'Add those as well
                    catalog.Catalogs.Add(New DirectoryCatalog(addloc, "*.dll"))
                End If
        End If

        'Create the CompositionContainer with the parts in the catalog
        _container = New CompositionContainer(catalog)

        'Fill the imports of this object

           'If that was successful, then we know everything loaded, let's grab the filenames
            For Each cat In catalog.Catalogs
                'Check to make sure that the Catalog is a Directory Catalog (There are multiple TYPES)
                If TypeOf cat Is DirectoryCatalog Then
                    'Let's create a DirectoryCatalog and merge it with the contents of the current Catalog
                    Dim catdir As DirectoryCatalog = cat
                    'If the file loaded correctly then continue
                    If catdir.LoadedFiles.Count > 0 Then
                        'Grab each file individually
                        For Each lf In ids.LoadedFiles
                            'MessageBox the result for us to what you will with it though
                        'Loop Back Through Each DLL
                    End If
                Else 'Wasnot a Directory Catalog. The only other Catalog we coded for was an Assembly
                    Dim asm As AssemblyCatalog = i
                    'Again lets message out that location as well.
                End If
          'Loop back through again  
        Catch ref As ReflectionTypeLoadException
            For Each ex In ref.LoaderExceptions
        End Try


Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.