【发布时间】:2010-05-05 20:44:26
【问题描述】:
我正在定义以下类型扩展:
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 的静态强制涉及基于此程序点之前的信息的不确定类型。某些类型不允许使用静态强制。需要进一步的类型注释。
我尝试过注释函数返回类型,但得到了相同的结果。我不想将其更改为动态演员表。有没有办法让静态演员工作?
【问题讨论】:
标签: f#