F#自定义类型提供程序:“容器类型已设置”错误

时间:2012-06-23 14:51:16

标签: f# type-providers

最近我参加了Keith Battochi关于类型提供程序的教程,其中他在MSDN教程中介绍了MiniCsv类型提供程序的变体。不幸的是,我的笔记本电脑不可用,所以我不得不手工写下代码。我相信我已经重新创建了类型提供程序,但我得到了

error FS3033: The type provider 'CsvFileTypeProvider+CsvFileTypeProvider' reported an error: container type for 'CsvFileProvider.Row' was already set to 'CsvFileProvider.CsvFile,Filename="events.csv"

当我查看代码时,我无法看到我如何将Row类型添加到容器两次(或者其他容器)。删除选定的代码行无济于事。

以下是我在fsi中调用代码的方式:

#r "CsvFileTypeProvider.dll"
open CsvFileProvider
let eventInfos = new CsvFile<"events.csv">() ;;

这是代码本身:

module CsvFileTypeProvider
open Samples.FSharp.ProvidedTypes
open Microsoft.FSharp.Core.CompilerServices

let getType str =
    if System.DateTime.TryParse(str, ref Unchecked.defaultof<_>) then
        typeof<System.DateTime>, (fun (str:Quotations.Expr) -> <@@ System.DateTime.Parse(%%str) @@>)
    elif System.Int32.TryParse(str, ref Unchecked.defaultof<_>) then
        typeof<System.Int32>, (fun (str:Quotations.Expr) -> <@@ System.Int32.Parse(%%str) @@>)
    elif System.Double.TryParse(str, ref Unchecked.defaultof<_>) then
        typeof<System.Double>, (fun (str:Quotations.Expr) -> <@@ System.Double.Parse(%%str) @@>)
    else
        typeof<string>, (fun (str:Quotations.Expr) -> <@@ %%str @@>)

[<TypeProvider>]
type CsvFileTypeProvider() =
    inherit TypeProviderForNamespaces()

    let asm = typeof<CsvFileTypeProvider>.Assembly
    let ns = "CsvFileProvider"

    let csvFileProviderType = ProvidedTypeDefinition(asm, ns, "CsvFile", None)
    let parameters = [ProvidedStaticParameter("Filename", typeof<string>)]

    do csvFileProviderType.DefineStaticParameters(parameters, fun tyName [| :? string as filename |] ->
        let rowType = ProvidedTypeDefinition(asm, ns, "Row", Some(typeof<string[]>))

        let lines = System.IO.File.ReadLines(filename) |> Seq.map (fun line -> line.Split(','))
        let columnNames = lines |> Seq.nth 0
        let resultTypes = lines |> Seq.nth 1 |> Array.map getType

        for idx in 0 .. (columnNames.Length - 1) do
            let col = columnNames.[idx]
            let ty, converter = resultTypes.[idx]
            let prop = ProvidedProperty(col, ty)
            prop.GetterCode <- fun [row] -> converter <@@  (%%row:string[]).[idx] @@>
            rowType.AddMember(prop)

        let wholeFileType = ProvidedTypeDefinition(asm, ns, tyName, Some(typedefof<seq<_>>.MakeGenericType(rowType)))
        wholeFileType.AddMember(rowType)

        let ctor = ProvidedConstructor(parameters = []) // the *type* is parameterized but the *constructor* gets no args
        ctor.InvokeCode <- //given the inputs, what will we get as the outputs? Now we want to read the *data*, skip the header
            fun [] -> <@@ System.IO.File.ReadLines(filename) |> Seq.skip 1 |> Seq.map (fun line -> line.Split(','))  @@>
        wholeFileType.AddMember(ctor)
        wholeFileType
        )

    do base.AddNamespace(ns, [csvFileProviderType])

[<TypeProviderAssembly>]
do()

感谢您的帮助!

1 个答案:

答案 0 :(得分:6)

在定义&#39; Row&#39;时需要使用其他构造函数。类型。现有的ProvidedTypeDefinition类型公开两个构造函数:

  • (assembly,namespace,typename,base type) - 定义容器为命名空间的顶级类型。
  • (typename,basetype) - 定义应添加到其他类型的嵌套类型。

现在使用第一个构造函数定义行类型,因此它被视为顶级类型。稍后将此类型作为嵌套添加到wholeFileType时会引发异常。