简体   繁体   English

为什么我的F#函数的返回类型为bool?

[英]Why is my F# function's return type bool?

I am writing a function that takes a PropertyInfo object, fetches MyAttribute and returns a MyAttribute object: 我正在编写一个接受PropertyInfo对象,获取MyAttribute并返回MyAttribute对象的函数:

let getparamattribute(p : PropertyInfo) = 
    let attr = p.GetCustomAttribute (typeof<MyAttribute>, true) 
    attr :? MyAttribute 

However FSI shows the return type as bool: 但是FSI将返回类型显示为bool:

val getparamattribute : (PropertyInfo -> bool)

Why? 为什么?

That's exactly what :? 那就是:? operator does: 操作员可以:

Returns true if the value matches the specified type; 如果值与指定的类型匹配,则返回true否则,返回true otherwise, returns false (type test operator). 否则,返回false (类型测试运算符)。

What you're looking for is :?> downcast operator: 您正在寻找的是:?>下垂运算符:

The :?> operator performs a dynamic cast, which means that the success of the cast is determined at run time. :?>运算符执行动态转换,这意味着转换的成功取决于运行时。 A cast that uses the :?> operator is not checked at compile time; 使用:?>运算符的类型转换在编译时不会检查; but at run time, an attempt is made to cast to the specified type. 但是在运行时,会尝试将其强制转换为指定的类型。 If the object is compatible with the target type, the cast succeeds. 如果对象与目标类型兼容,则转换成功。 If the object is not compatible with the target type, the runtime raises an InvalidCastException . 如果对象与目标类型不兼容,则运行时将引发InvalidCastException

from Symbol and Operator Reference 来自Symbol and Operator Reference

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM