我在运行时使用MEF组合了许多组件,但是当我使用元数据属性时,它会使集合中的部件数量增加一倍。
当检查container.ComposeParts时,我可以看到包含11个IStages实现的dll被加载一次,只有11个部分需要出现但是当它解析私有属性阶段为IEnumerable时(Lazy(Of IStages,IStagesMetadata)) )实例数加倍到22.通过集合循环我可以看到它们被元数据中的ID复制。
如果我在导出或导入时不使用Metadataattribute,那么我会得到预期的11个部分。
导出属性
Imports System.ComponentModel.Composition
<MetadataAttribute(), AttributeUsage(AttributeTargets.Class, AllowMultiple:=False)>
Public Class StagesMetadataAttribute
Inherits ExportAttribute
Public Property StageID As Byte
Public Property Version As String
Public Sub New()
MyBase.New(GetType(IStages))
End Sub
End Class
部分示例
Imports System.ComponentModel.Composition
Imports VFRAME.QUALITY.GOODSIN.ESCALATE.INTERFACES
<Export(GetType(IStages))>
<StagesMetadata(StageID:=1, Version:="v1.0.0.0")>
Public Class Stage1
Implements IStages
Public Function ProcessEscalation(failure As InspectionFaultsModel) As InspectionFaultsModel Implements IStages.ProcessEscalation
Return Nothing
End Function
End Class
导入侧构造函数
Using catelog As New DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory)
Using container As New CompositionContainer(catelog)
Try
container.ComposeParts(Me)
Catch ex As CompositionException
_compositionComplete = False
End Try
End Using
End Using
ImportMany
<ImportMany()>
Private Property Stages As IEnumerable(Of Lazy(Of IStages, IStagesMetadata))
导入元记录界面
Public Interface IStagesMetadata
ReadOnly Property StageID As Byte
ReadOnly Property Version As String
End Interface
知道问题是什么吗?
答案 0 :(得分:1)
想出来
如果使用Export和StagesMetadata装饰零件,它会为零件创建2个导出定义,因为StagesMetadataAttribute继承自ExportAttribute
<Export(GetType(IStages))>
<StagesMetadata(StageID:=1, Version:="v1.0.0.0")>
Public Class Stage1
Implements IStages
Public Function ProcessEscalation(failure As InspectionFaultsModel) As InspectionFaultsModel Implements IStages.ProcessEscalation
Return Nothing
End Function
End Class
Theres没有说明愚蠢。