.net F中重复的MEF导出定义#

.net F中重复的MEF导出定义#,.net,f#,mef,.net,F#,Mef,我有一个相对简单的程序: open System open System.ComponentModel.Composition open System.ComponentModel.Composition.Hosting open System.Reflection module Config = [<Export("Timer.Delay")>] let Delay = TimeSpan.FromSeconds(5.0) [<Entry

我有一个相对简单的程序:

open System
open System.ComponentModel.Composition
open System.ComponentModel.Composition.Hosting
open System.Reflection

module Config =
    [<Export("Timer.Delay")>]
    let Delay = TimeSpan.FromSeconds(5.0)

[<EntryPoint>]
let main argv = 
    let catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly())
    let container = new CompositionContainer(catalog)
    let delay = container.GetExportedValue<TimeSpan> "Timer.Delay"

    printfn "Delay: %A" delay
开放系统
开放系统.ComponentModel.Composition
开放系统.ComponentModel.Composition.Hosting
开放系统。反射
模块配置=
[]
让延迟=时间跨度从秒(5.0)
[]
让主argv=
let catalog=new AssemblyCatalog(Assembly.getExecutionGassembly())
let container=新合成容器(目录)
让delay=container.GetExportedValue“Timer.delay”
printfn“延迟:%A”延迟
但是我在调用
container.GetExportedValue“Timer.Delay”
时出错:

找到多个与约束匹配的导出:

 合同名称     定时器,延时

 必需类型标识 System.TimeSpan

查看
catalog.Parts
集合,我看到两个部分,每个部分都有一个
ExportDefinition
。第一个用于
Program+Config
,我希望找到它,另一个用于
$Program
(注意程序集名称是
Remote
):


模块程序中包装
main
函数不会改变此行为,也不会将这些模块分离到不同的文件中。有人知道为什么这个F#程序会生成第二个导出定义吗?我怎样才能防止这种情况发生呢?

在对它进行了一点修改之后,我想我已经找到了答案。我的猜测是,当F#编译器在类似上面的属性上看到一个非目标属性时,它实际上会发出两个属性,一个用于属性,一个用于字段。
ExportAttribute
可能也适用于属性和字段。在属性上指定目标似乎可以解决此问题

这将生成
程序+配置
部分:

module Config =
    [<property: Export("Timer.Delay")>]
    let Delay = TimeSpan.FromSeconds(5.0)
module Config =
    [<field: Export("Timer.Delay")>]
    let Delay = TimeSpan.FromSeconds(5.0)
模块配置=
[]
让延迟=时间跨度从秒(5.0)
这将生成
$程序
部分:

module Config =
    [<property: Export("Timer.Delay")>]
    let Delay = TimeSpan.FromSeconds(5.0)
module Config =
    [<field: Export("Timer.Delay")>]
    let Delay = TimeSpan.FromSeconds(5.0)
模块配置=
[]
让延迟=时间跨度从秒(5.0)
这两种方法都可以解决我的问题,但第一种方法似乎更好,因为它将
Export
属性绑定到实际模块的一个成员,而不是一些编译器生成的代码