This project has moved and is read-only. For the latest updates, please go here.

Generic Import & Metadata

May 18, 2010 at 8:10 PM
Edited May 18, 2010 at 8:20 PM

According to this post MEF doesn't support InheritedExport's with Metadata on the inheriting class:

Is there is another way to have a Generic Import that also has metadata attached to it since attributes (<ImportMany(Gettype(T)>don't support generics)?  See below for an example.

{see next post}
May 18, 2010 at 8:17 PM
Edited May 18, 2010 at 8:23 PM

Looks like the code didn't insert correctly it should match this:

Public Interface IImport
End Interface

<System.ComponentModel.Composition.InheritedExport()> _
Public Interface ICustomImport
Inherits IImport
End Interface

<PriorityMetadata(2)> _
Public Class CustomImport1
Implements ICustomImport

End Class

<PriorityMetadata(1)> _
Public Class CustomImport2
Implements ICustomImport

End Class

<System.ComponentModel.Composition.MetadataAttribute()> _
<AttributeUsage(AttributeTargets.Class,AllowMultiple:=False)> _ Public Class PriorityMetadataAttribute Inherits Attribute Implements IPriorityMetadata Public Sub New(ByVal priority As Integer) _Priority = priority End Sub Private ReadOnly _Priority As Integer Public ReadOnly Property Priority As Integer Implements IPriorityMetadata.Priority Get Return _Priority End Get End Property End Class Public Interface IPriorityMetadata ReadOnly Property Priority As Integer End Interface Public Class BaseImporter(Of T As IImport) <System.ComponentModel.Composition.ImportMany()> _ Public Property CustomImports As ObservableCollection(Of Lazy(Of T, IPriorityMetadata)) End Class Public Class Importer Inherits BaseImporter(Of ICustomImport) Public Sub New() System.ComponentModel.Composition.CompositionInitializer.SatisfyImports(Me) '0 Records returned in CustomImports as long as Lazy(Of T) includes IPriorityMetadata End Sub End Class


May 23, 2010 at 8:32 PM

MEF Contrib supports this. See