繁体   English   中英

FileHelpers BadUsageException仅在编译后的F#上有效,而在脚本中则没有

[英]FileHelpers BadUsageException only on compiled F# but not in script

我写了一个fsi脚本,效果很好,并希望对其进行编译,以便我可以更轻松地移动它。 但是,当我编译它时,突然,FileHelpers开始出现错误。

以下代码使用FileHelpers 2.9.9 这是一个最小的工作示例,用于说明问题test.fsx

#r "FileHelpers.dll"

open FileHelpers

[<DelimitedRecord(",")>]
type Type = 
    val field1 : string
    val field2 : int
    override x.ToString() = sprintf "%s: %d" x.field1 x.field2

let readFile<'a> file = seq {
    use engine1 = new FileHelperAsyncEngine(typeof<'a>)
    use tmp1 = engine1.BeginReadFile(file)

    engine1.ReadNext() |> ignore

    while engine1.LastRecord <> null do
        yield engine1.LastRecord :?> 'a
        engine1.ReadNext() |> ignore
    }


readFile<Type> "test.csv" |> Seq.iter (printfn "%A")

使用文件test.csv作为

test1,1
test2,2
test3,3

如果我将代码作为fsi .\\test.fsx运行,它将可以正常工作。 但是,如果尝试使用fsc .\\test.fsx进行编译并运行.\\test.exe Unhandled Exception: FileHelpers.BadUsageException: The record class Type needs a constructor with no args (public or private)出现错误: Unhandled Exception: FileHelpers.BadUsageException: The record class Type needs a constructor with no args (public or private) 在脚本和编译模式下均有效的解决方法是

[<DelimitedRecord(",")>]
type Type () = 
    [<DefaultValue>]
    val mutable field1 : string
    [<DefaultValue>]
    val mutable field2 : int
    override x.ToString() = sprintf "%s: %d" x.field1 x.field2

为什么将其用作脚本但未编译? 如果可能的话,我想保持不变。 感谢您的见解!

FSI使用System.Reflection.Emit即时编译您的F#代码。 似乎使用System.Reflection.Emit生成的类型始终至少具有一个构造函数(默认的公共构造函数或显式定义的构造函数)。 因此,FSI发出的代码要完全模仿完全没有构造函数(既不是公共的也不是私有的)的编译代码的结果,是不可能的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM