F#错误:参数数量错误

时间:2009-01-13 22:34:42

标签: f#

我在以前的SO问题中给出的一些代码令人费解。 Code here

在这一行,我收到一条错误消息,说我的参数数量无效。我不完全理解错误是什么,因为我所做的所有研究都认为这是函数的正确应用。

let result = Seq.to_list(Microsoft.FSharp.Compatibility.Seq.generate_using opener generator)

这里发生了什么?为什么我会遇到这种错误?

编辑:我使用PowerPack.dll引用和MySQL.Data引用

开启者和代码的代码发电机如下:

 let opener() = 
        let command = connection.CreateCommand(CommandText = sql, CommandType = System.Data.CommandType.Text)
        command.ExecuteReader()

和发电机......

let generator<'a> (reader : System.Data.IDataReader) =
    if reader.Read() then
        let t = typeof<'a>
        let props = t.GetProperties()
        let types = props
                    |> Seq.map (fun x -> x.PropertyType)
                    |> Seq.to_array
        let cstr = t.GetConstructor(types)
        let values = Array.create reader.FieldCount (new obj())
        reader.GetValues(values) |> ignore
        let values = values
                     |> Array.map (fun x -> match x with | :? System.DBNull -> null | _ -> x)
        Some (cstr.Invoke(values) :?> 'a)
    else
        None

2 个答案:

答案 0 :(得分:1)

这篇文章陈旧且萎靡不振,但您确定参数的无效错误数量不是来自Oracle命令吗?

基本上:您是在SQL查询中指定参数吗?开启功能不应用任何参数。

答案 1 :(得分:0)

以下类型为我检查:

#light
open System.Data
open System.Data.SqlClient
open System.Configuration


type Employee =
    { FirstName: string;
      LastName: string; }

let generator<'a> (reader: IDataReader) =
    if reader.Read() then
        let t = typeof<'a>
        let props = t.GetProperties()
        let types = props
                    |> Seq.map (fun x -> x.PropertyType)
                    |> Seq.to_array
        let cstr = t.GetConstructor(types)
        let values = Array.create reader.FieldCount (new obj())
        reader.GetValues(values) |> ignore
        let values = values
                     |> Array.map (fun x -> match x with | :? System.DBNull -> null | _ -> x)
        Some (cstr.Invoke(values) :?> 'a)
    else
        None

let opener() = 
    let sql = "select * from employees"
    let connection = new SqlConnection(ConfigurationManager.ConnectionStrings.["myConnection"].ConnectionString)
    let command = connection.CreateCommand(CommandText = sql, CommandType = System.Data.CommandType.Text)
    command.ExecuteReader()

let result = Seq.to_list(Microsoft.FSharp.Compatibility.Seq.generate_using opener (generator<Employee>))

看起来你错过了一个类型注释来告诉生成器你生成什么类型​​的列表,即:generator<Employee>