MEF CompositionContainer缺少ComposeParts扩展方法

时间:2012-09-07 09:16:19

标签: .net vb.net extension-methods mef .net-4.5

我试图将MEF组成部分抽象为类库类,但我在.Net4.5 RC中遇到问题,因为ComposeParts Extension方法在类库CompositionContainer中不可用

我做错了什么或者错过了参考?

WCF服务类工作容器

<Import(GetType(IGenericModelFactory))>
Public Property ModelFactory As IGenericModelFactory

Private _mefContainer As CompositionContainer
Private _catelog As DirectoryCatalog

Public Sub New()
    _catelog = New DirectoryCatalog(".")
    _mefContainer = New CompositionContainer(_catelog)
    _mefContainer.ComposeParts(Me)
End Sub

类库 - 缺少ComposeParts方法

Imports System.ComponentModel.Composition.Hosting

Namespace Core.Composition

    Public Class CompositionBuilder
        Implements IComposeObjects

        Public Function CreateContainer() As CompositionContainer Implements IComposeObjects.CreateContainer
            Return Activator.CreateInstance(Of CompositionContainer)()
        End Function

        Public Sub ComposeFromDirectory(directory As String, compositionRoot As Object) Implements IComposeObjects.ComposeFromDirectory

            Dim catelog As DirectoryCatalog = Activator.CreateInstance(GetType(DirectoryCatalog), directory)
            Dim mefContainer As CompositionContainer = Activator.CreateInstance(GetType(CompositionContainer), catelog)
            mefContainer.Composeparts(compositionRoot)

        End Sub

    End Class

End Namespace

enter image description here

1 个答案:

答案 0 :(得分:2)

看起来您必须包含System.ComponentModel.Composition命名空间

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

Namespace Core.Composition

    Public Class CompositionBuilder
        Implements IComposeObjects

        Public Function CreateContainer() As CompositionContainer Implements IComposeObjects.CreateContainer
            Return Activator.CreateInstance(Of CompositionContainer)()
        End Function

        Public Sub ComposeFromDirectory(directory As String, compositionRoot As Object) Implements IComposeObjects.ComposeFromDirectory

            Dim catelog As DirectoryCatalog = Activator.CreateInstance(GetType(DirectoryCatalog), directory)
            Dim mefContainer As CompositionContainer = Activator.CreateInstance(GetType(CompositionContainer), catelog)
            mefContainer.ComposeParts(compositionRoot)

        End Sub

    End Class

End Namespace