Using MEF with VB

Mar 13, 2009 at 6:52 PM
I have been bouncing around the net looking for something that would allow me to dynamically load and use user controls.  I was directed to MEF and got excited watching the MEF example at PDC2008 and this is what I need.  However, I am a VB developer and work in a VB shop.  I understand that MEF is just a framework that is compiled and referenced and that you tag your imports and exports.  I am having a problem following the usage in C#.  Are there any good VB examples on how to use this?  Specifically I have a core program (the Host) that will contain the common functionality amoung all the parts that will be imported.  So I need to create a host that exposes these functions, the parts need to display themselves as user controls and access these common functions.  Also the host needs to communicate with the parts to set properties.  So if someone has a link or document of how to use VB with MEF so I can start using this design pattern.

Thanks.

John
Mar 15, 2009 at 3:40 PM
I cannot point you at VB documentation right now, remember MEF is in a very preliminary state and honestly the C# docs are pretty limited as well.

However, I can tell you that I use MEF with VB and kudos to the MEF team that there have been no problems.

I will be releasing a composable system for code generation very soon which you may explore as a reference applicatoin.

I think starting up in MEF in either language today is challenging becuase there is not delineation between what you can do and what you should do. I know the team is working on more guidance, and I'm recording some of my explorations at my blog.Leaning Into Windows
Mar 23, 2009 at 7:19 PM
Thank you for the response. I would be interested in anything you can show me in VB. This is a very promising architecture to use if I could get more direction. I am looking forward to anything you can show me.
John
----- Original Message -----
From: [email removed]
To: [email removed]
Sent: Sunday, March 15, 2009 9:40 AM
Subject: Re: Using MEF with VB [MEF:50143]

From: KathleenDOllard

I cannot point you at VB documentation right now, remember MEF is in a very preliminary state and honestly the C# docs are pretty limited as well.

However, I can tell you that I use MEF with VB and kudos to the MEF team that there have been no problems.

I will be releasing a composable system for code generation very soon which you may explore as a reference applicatoin.

I think starting up in MEF in either language today is challenging becuase there is not delineation between what you can do and what you should do. I know the team is working on more guidance, and I'm recording some of my explorations at my blog.Leaning Into Windows
Mar 29, 2009 at 9:55 AM
Edited Mar 29, 2009 at 9:57 AM
Here is example code:

Imports System.ComponentModel.Composition
Imports System.ComponentModel.Composition.Hosting


Public Interface IAddin
	Public ReadOnly Property GetUserControl() as System.Windows.Forms.UserControl
End Interface

<Export(GetType(IAddin))> _
<ExportMetadata("Title", "My First Add-in")> _
<ExportMetadata("Version", "1.0.0.0")> _
Public Class Addin1
	Inherits IAddin

	Public ReadOnly Property GetUserControl() as System.Windows.Forms.UserControl
		return nothing
	End Property

End Class

<Export(GetType(AddInHost))> _
Public Class AddInHost
	
    Public container As CompositionContainer = Nothing

    Public Sub Compose()
        Dim sPathInitial As String = My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData

        Dim sPath As String = IO.Path.Combine(sPathInitial, "Extensions")

        'Debug.WriteLine(sPath)

        If IO.Directory.Exists(sPath) = False Then
            IO.Directory.CreateDirectory(sPath)
        End If
        Dim catalog As New AggregateCatalog()

        ' Directory of plugins
        catalog.Catalogs.Add(New DirectoryCatalog(sPath))

        ' add me in there too
        catalog.Catalogs.Add(New AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()))

        container = New CompositionContainer(catalog)
        container.Compose(New CompositionBatch())

        If container.Catalog.Parts.Count > 0 Then

            Dim x As System.Collections.ObjectModel.Collection(Of IAddin) = container.GetExportedObjects(Of IAddin)()

            Dim ex As ExportCollection(Of IAddin) = container.GetExports(Of IAddin)()

            If ex.Count > 0 Then
                Debug.WriteLine("")
                Debug.WriteLine(GetMetadataValue(ex.Item(0).Metadata, "Title"))
                Debug.WriteLine(GetMetadataValue(ex.Item(0).Metadata, "Version"))
                Debug.WriteLine("")
            End If

        End If

    End Sub

    Public Function GetMetadataValue(ByVal m As Generic.IDictionary(Of String, Object), ByVal key As String) As String
        Dim value As String = String.Empty
        Dim oVal As Object = Nothing

        m.TryGetValue(key, oVal)
        If Not oVal Is Nothing Then value = oVal.ToString()

        Return value
    End Function

End Class
Developer
Mar 30, 2009 at 4:19 PM
FYI: The following line from the code sample is not needed and does nothing:
container.Compose(New CompositionBatch())

I would just remove it. If you are only dealing with catalogs then you don't ever need to call compose directly. It should only be used if you are adding objects/parts to the container manually.