4

我正在编写一个适配器类来将 IEnumerable<'T> 映射到 IDataReader 完整源代码位于https://gist.github.com/jsnape/56f1fb4876974de94238以供参考,但我想询问编写其中一部分的最佳方法. 即两个功能:

member this.GetValue(ordinal) =
    let value = match enumerator with
        | Some e -> getters.[ordinal](e.Current)
        | None -> raise (new ObjectDisposedException("EnumerableReader"))

    match value with
        | :? Option<string> as x -> if x.IsNone then DBNull.Value :> obj else x.Value :> obj
        | :? Option<int> as x -> if x.IsNone then DBNull.Value :> obj else x.Value :> obj
        | :? Option<decimal> as x -> if x.IsNone then DBNull.Value :> obj else x.Value :> obj
        | :? Option<obj> as x -> if x.IsNone then DBNull.Value :> obj else x.Value
        | _ -> value

此函数必须返回一个对象,但由于传递的值可以是下游函数(如 SqlBulkCopy)无法理解的任何 F# 选项类型,因此我需要解压缩选项并将其转换为 null/DBNull。

上面的代码有效,但我觉得它有点笨拙,因为我必须为不同的类型(float 等)添加新的专业化。我确实尝试过使用通配符 | :? Option <_> as x -> 在匹配中,但编译器给了我一个“不太通用的警告”,并且代码只会匹配 Option< obj >。

这怎么能写得更惯用呢?我怀疑主动模式可能会起作用,但我从未使用过它们。

对于这个其他功能也是如此:

member this.IsDBNull(ordinal) =
    match (this :> IDataReader).GetValue(ordinal) with
        | null -> true
        | :? DBNull -> true
        | :? Option<string> as x -> x.IsNone
        | :? Option<int> as x -> x.IsNone
        | :? Option<decimal> as x -> x.IsNone
        | :? Option<obj> as x -> x.IsNone
        | _ -> false

我不在乎它是什么类型的 Option 我只想检查 IsNone

4

2 回答 2

5

我认为你应该使用一些这样的反射技术:

open System

let f (x:obj) =
    let tOption = typeof<option<obj>>.GetGenericTypeDefinition()
    match x with
    | null -> printfn "null"; true
    | :? DBNull -> printfn "dbnull"; true
    | _ when x.GetType().IsGenericType && x.GetType().GetGenericTypeDefinition() = tOption ->
        match x.GetType().GenericTypeArguments with
        | [|t|] when t = typeof<int> -> printfn "option int"; true
        | [|t|] when t = typeof<obj> -> printfn "option obj"; true
        | _                          -> printfn "option 't" ; true

    | _ -> printfn "default"; false


let x = 4 :> obj
let x' = f x  //default

let y = Some 4 :> obj
let y' = f y  // option int

let z = Some 0.3 :> obj
let z' = f z  // option 't

更新

事实上,如果您只是想检查所有选项类型的 IsNone 情况,并且不想使用反射,那么您不需要其他情况,因为 None 被编译为 null,它们将属于 null 情况。例如,使用前面的函数试试这个:

let y1 = (None: int option)  :> obj
let y1' = f y1  // null

let z1 = (None: float option)  :> obj
let z1' = f z1  // null

它正在与第一个案例(空案例)一起处理

对于 GetValue 成员,我查看了您的要点,因为您已经在包含该成员的类型中定义了通用 'T,您可以编写:

match value with
| :? Option<'T> as x -> if x.IsNone then DBNull.Value :> obj else x.Value :> obj

适用于所有选项类型。

于 2014-07-19T18:41:21.357 回答
2

正如古斯塔沃的回答所暗示的那样,您应该为此使用反射。如果参数在编译时未知,则没有其他方法可以将类型obj转换为类型。相反,您必须将参数作为一个对象进行检查,然后决定下一步该做什么。option<'a>'aSystem.Type

这样做的一般方法是设置一个可以将任何选项类型作为参数的函数,并返回其类型不依赖于选项类型的参数的东西。然后可以在确定参数类型后通过反射调用此函数。

要定义可以将任何选项类型作为参数的函数,辅助接口很有用,因为我们可以在该接口内定义一个泛型方法:

type IGenericOptionHandler<'result> =
    abstract Handle<'a> : 'a option -> 'result

请注意,整个接口对于方法的返回类型是泛型的,内部参数仅在方法本身的定义中提及。'resultHandle'a

我们现在可以定义一个函数来调用这个接口:

let handleGeneric
        (handle : IGenericOptionHandler<'result>)
        (x : obj)  // something that might be an option type
        (defaultValue : 'result) // used if x is not an option type
      : 'result =

    let t = x.GetType()
    if t.IsGenericType && t.GetGenericTypeDefinition() = typedefof<_ option>
        then
            match t.GetGenericArguments() with
            | [|tArg|] ->
                handle
                  .GetType()
                  .GetMethod("Handle")
                  .MakeGenericMethod([|tArg|])
                  .Invoke(handle, [|x|])
                 :?> 'result
            | args -> failwith "Unexpected type arguments to option: %A" args
        else defaultValue

最后,我们可以使用对象表达式方便地调用它,例如,以下将充当类似于IsDBNull上面的通用选项类型检测器 - 您需要DBNulldefaultValue参数中添加特殊情况才能准确复制它。

Option.handleGeneric
    { new IGenericOptionHandler<bool> with member this.Handle _ = true }
    (Some 5)
    false
于 2014-07-19T20:09:15.160 回答