Quantcast
Channel: Managed Extensibility Framework
Viewing all articles
Browse latest Browse all 265

New Post: Using MEF with recursion and TreeView

$
0
0
Not sure if you figured it out for yourself, but I posted an idea on my blog which I'll share here. It's in VB though.
'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
            Next
        End If

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

        'Fill the imports of this object
        Try
            _container.ComposeParts(Me)

           '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 see....do what you will with it though
                            MsgBox(lf.ToString)
                        'Loop Back Through Each DLL
                        Next
                    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.
                    MsgBox(asm.Assembly.Location)
                End If
          'Loop back through again  
          Next
        Catch ref As ReflectionTypeLoadException
            For Each ex In ref.LoaderExceptions
                MsgBox(ex.Message)
            Next
        End Try
So basically, grab your AggregateCatalog that you created ("catalog" object in my code),
then do a For Each loop on the Catalogs subobject (catalog.Catalogs)
foreach ( void cat in catalog.Catalogs){

}
Then check what kind of Catalog you have, as they have different properties
if (cat is DirectoryCatalog) {
        DirectoryCatalog catdir = cat;
        if (catdir.LoadedFiles.Count > 0) {
            foreach (void filename in ids.LoadedFiles) {
                Interaction.MsgBox(filename.ToString);  //DirectoryCatalog . LoadedFiles ( index# )  will return the Full Path / Filename of the Loaded .dll from directories. 
            }
        }
    } else {
        AssemblyCatalog asm = cat;
        Interaction.MsgBox(asm.Assembly.Location); // AssemblyCatalog . Assembly . Location will produce a string for the full Path/Filename of the loaded Assembly
    }
Hope that helps, sorry if the C# is wrong in any way, I am more of a vb.net programmer than C#.
Cheers

Viewing all articles
Browse latest Browse all 265

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>