F#类型提供程序相关的嵌套类型

时间:2018-11-11 12:58:04

标签: f# type-providers dependent-type f#-data

我正在尝试构建一个TypeProvider,其中根据父对象的类型值生成嵌套的ProvidedProperty

我想要得到的结果是

#r @"bin/Debug/library.dll"
open Library.TypeProviders

type sdmx = SdmxDataProvider<WsEntryPoint="">
let dataflows = sdmx.GetDataFlowsContext()

let a = dataflows.A.A1 // A1,A2,A3 < should be generated based on A
let b = dataflows.B.B1 // B1,B2,B3 < should be generated based on B
let c = dataflows.C.C1 // C1,C2,C3 < should be generated based on C

我可以为所有dimensionsType生成所有dataflowsType

enter image description here

然后我尝试添加参数dfId,然后使用它来选择属性。

let dimensionsType dfId =
    let t = ProvidedTypeDefinition("Dimensions", Some typeof<string>)
    t.AddMembersDelayed (fun () -> 
        [ for dimension in [(1, "A1"); (1, "A2"); (1, "A3");
                            (2, "B1"); (2, "B2"); (2, "B3");
                            (3, "C1"); (3, "C2"); (3, "C3")] do
              let dataflowId, dimensionName = dimension
              if dataflowId = dfId then
                  let prop = 
                      ProvidedProperty
                        ( dimensionName, typeof<string>, 
                          GetterCode = (fun args -> <@@ (%%args.[0]) :> string @@>))
                  yield prop                     
        ])
    serviceTypesType.AddMember t
    t

let dataflowsType =
    let t = ProvidedTypeDefinition("Dataflows", Some typeof<string>)
    t.AddMembersDelayed (fun () ->
        [ for dataflow in [(1, "A"); (2, "B") ; (3, "C")] do
            let dataflowId, dataflowName = dataflow
            let prop = ProvidedProperty(dataflowName, dimensionsType dataflowId, GetterCode = (fun args -> <@@ (%%args.[0]) :> string @@>))
            yield prop])
    serviceTypesType.AddMember t
    t

此方法会引发错误:error FS1109: A reference to the type 'Library.TypeProviders.SdmxDataProvider,WsEntryPoint="".ServiceTypes.Dimensions' in assembly 'Library' was found, but the type could not be found in that assembly

是否可以进行这种类型的生成?

完整来源:https://gist.github.com/demonno/f9234ced664439986597cf7c3218d3b8

0 个答案:

没有答案