Using MEF and querying the catalog container with VB.NET

I am using the Managed Extensibility Framework preview 4 in VB.NET, and I have to say, that it rocks!

The project I am writing is a UI container that has “add-ins” that process files and such. I am working with another programmer to develop other add-ins for the app as well.

To query for specific metadata, we first have to create an interface and a custom attribute.

Public Interface IMefMetadataView
    ReadOnly Property Name() As String
End Interface
<MetadataAttribute()> _
Public Class MefMetadata
    Inherits Attribute
    Public Sub New()
    End Sub
    Public Sub New(ByVal name As String)
        _Name = name
    End Sub
    Private _Name As String = ""
    Public ReadOnly Property Name() As String
        Get
            Return _Name
        End Get
    End Property
End Class

This attribute allows you to specify a name of the add-in. Then we create an interface for our add-in.

    Public Interface IAddin
    End Interface

Then we can create an add-in or two.

	<Export(GetType(IAddin))> _
	<AddinMetadata("Add-in one")> _
	Public Class Addin1
	    Implements IAddin
	
	End Class
	
	<Export(GetType(IAddin))> _
	<AddinMetadata("Add-in two")> _
	Public Class Addin2
	    Implements IAddin
	
	End Class

Now, in our host user interface, we create a catalog and a container. Specifically we are creating an AggregateCatalog (a catalog of catalogs if you will) and then creating a catalog for just our assembly, then we are adding it into the container.

    Dim Container as CompositionContainer
    Dim Catalog As New AggregateCatalog()
    Dim AssemCatalog As New AssemblyCatalog(GetType(Module1).Assembly)
    Catalog.Catalogs.Add(AssemCatalog)
    Container = New CompositionContainer(Catalog)

Querying:

Get a list of addins:

    Dim Addins As Generic.List(Of Export(Of IAddin, IMefMetadataView)) = _
        Container.GetExports(Of IAddin, IMefMetadataView).ToList()

Query for a specific add-in with a name in the metadata using a lambda function:

Dim x As Export(Of IAddin, IMefMetadataView) = Addins.Where(Function(p) String.Compare(p.Metadata("Name").ToString(), "Add-in one", True) = 0).Single()

Or

Dim x As Export(Of IAddin, IMefMetadataView) = Addins.Where(Function(p) String.Compare(p.MetadataView.Name, "Add-in one", True) = 0).Single()

The box directly above allows us to query strongly typed metadata, thanks to the IMefMetadataView Interface and the MefMetadata class.

kick it on DotNetKicks.com
5. April 2009 21:15 by Rick | Comments (549) | Permalink

Comments

About Rick

Rick lives in North Las Vegas. He loves his wife, kids, dog, motorcycle, music and programming. There ain't nothing else. Oh yeah, mountain dew!



Programming interests are geared towards multimedia. Platforms are asp.net, windows forms, and WPF.

Calendar

<<  March 2010  >>
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

RecentComments

Comment RSS