0

MEF には次の問題があります。

ホストが使用するインターフェース定義:

Public Interface IExecuteDoSomething
   Inherits IAddinSettings

   Event DataReceived As EventHandler(Of DataReceivedEventArgs)
   Function DoSomething() As Boolean

End Interface

Public Class DataReceivedEventArgs
   Inherits EventArgs

   Public Sub New(ByVal message As String)
      Me.Message = message
   End Sub

   Public Message As String
End Class

ホスト内の他のコードで必要な追加のインターフェイス:

    Public Interface IAddinSettings
        ReadOnly Property Setting() As AddinSettings
    End Interface

    Public Class AddinSettings
        Private _Name As String
        Public Property Name() As String
            Get
                Return _Name
            End Get
            Set(ByVal value As String)
                _Name = value
            End Set
        End Property

        Public Sub New(ByVal name As String)
            Me.Name = name
        End Sub
    End Class

エクスポートを提供するクラス:

    <Export(GetType(SharedLibrary.IExecuteDoSomething))> Public Class Class1
        Implements SharedLibrary.IExecuteDoSomething
        Implements SharedLibrary.IAddinSettings

        Private _Addinsettings As New SharedLibrary.Addinsettings("Test")

        Public Function DoSomething() As Boolean Implements SharedLibrary.IExecuteDoSomething.DoSomething
            MsgBox("i did something")
            Return True
        End Function


        Public Event DataReceived(ByVal sender As Object, ByVal e As SharedLibrary.DataReceivedEventArgs) Implements SharedLibrary.IExecuteDoSomething.DataReceived

        Public ReadOnly Property Setting() As SharedLibrary.AddinSettings Implements SharedLibrary.IAddinSettings.Setting
            Get
                Return  _Addinsettings
            End Get
        End Property
    End Class

ザ・ホスト:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim catalog As New Hosting.AggregateCatalog
        Dim d As New Hosting.DirectoryCatalog("..path to dlll..")
        catalog.Catalogs.Add(d)
        Dim container = New Hosting.CompositionContainer(catalog)
        Dim batch As New Hosting.CompositionBatch
        batch.AddPart(Me)
        container.Compose(batch)
        For Each dd In dos
            AddHandler dd.DataReceived, AddressOf testevent
        Next
    End Sub

    <Import()> Public dos As IEnumerable(Of SharedLibrary.IExecuteDoSomething)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each d In dos
            d.DoSomething()
        Next
    End Sub

    Private Sub testevent(ByVal sender As Object, ByVal e As SharedLibrary.DataReceivedEventArgs)
        MsgBox("Event received: " & e.Message)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dosomethingelse(DirectCast(dos, System.Collections.Generic.List(Of SharedLibrary.IAddinSettings))) 
    End Sub
    Private Sub Dosomethingelse(byval settings as IEnumerable(Of SharedLibrary.IAddinSettings))
    End Sub 

End Class

Button2_Click ルーチンが実行されるまではすべて正常に動作しているように見えますが、InvalidCastException が次の情報とともにスロー
されます1[SharedLibrary.IExecuteDoSomething]' to type 'System.Collections.Generic.List

インポートされたオブジェクトは両方のインターフェースを実装しているため、この問題を解決するにはどうすればよいですか?

4

1 に答える 1

1

あなたは実際に共分散の問題に直面しているのではないかと思います - これがこのような問題の典型的な原因です。AList<IFoo>は、 extendsの場合でもありませんList<IBar>IBarIFoo

.NET 3.5 を使用している場合、この問題を回避する最も簡単な方法は、 を削除しDirectCastて代わりに を使用することEnumerable.Castです。

Dosomethingelse(dos.Cast(Of SharedLibrary.IAddinSettings))
于 2009-03-13T10:38:12.160 に答える