我正在定义以下类型扩展:
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/