f# - 'indeterminate type' 在哪里?

标签 f#

我正在定义以下类型扩展:

type System.Reflection.MemberInfo with
    member x.GetAttribute<'T when 'T :> Attribute>(required, inherit') =
        match required, Attribute.GetCustomAttribute(x, typeof<'T>, inherit') with
        | true, null -> invalidOp (sprintf "Missing required attribute: %s" typeof<'T>.FullName)
        | _, attr -> attr :> 'T

最后一个匹配表达式 (attr :> 'T) 给出错误:

从 Attribute 到 'T 的静态强制转换涉及基于此程序点之前的信息的不确定类型。某些类型不允许静态强制转换。需要进一步的类型注释。

我尝试注释函数返回类型,但得到了相同的结果。我不想将其更改为动态 Actor 阵容。有没有办法让静态 Actor 工作?

最佳答案

这一定是一个动态的 Actor 阵容,对吧?您有一个静态类型为 System.Attribute 的对象,并且您希望将其向下转换为其实际的具体类型。

open System
type System.Reflection.MemberInfo with 
  member x.GetAttribute<'T when 'T :> Attribute>(required, inherit') =  // '
    match required, Attribute.GetCustomAttribute(x, typeof<'T>, inherit') with 
    | true, null -> invalidOp (
        sprintf "Missing required attribute: %s" typeof<'T>.FullName)  // '
    | _, attr -> attr :?> 'T 

关于f# - 'indeterminate type' 在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2766436/

相关文章:

c# - 我可以不使用 try catch() 来忽略错误吗?

f# - 获取字符串列表形式的记录类型标签

F#在从记录列表派生的 map 上应用 Map.filter

f# - 在 F# 中使用关键字作为成员名称

F# 递归函数在奇怪的无限循环中

visual-studio - Visual Studio 中使用 FSharp 的相对目录路径

list - 扩展 F# 列表模块

f# - 将变量绑定(bind)到 FSI 中的接口(interface)类型

f# - 涉及 “Expected incomplete structured construct”的级联错误

arrays - F# 比较两个数组是否相等到指定的精度级别