F#高级编程技术:反射、模式匹配与互操作性
1. 类型反射
反射的一个简单应用是使用 typeof 运算符访问类型和泛型类型变量的表示。例如, typeof<int> 和 typeof<'T> 都会生成 System.Type 类型的值。通过 System.Type 值,可以使用.NET API访问包含该类型定义的.NET程序集的 System.Reflection.Assembly 值,还能访问 System.Reflection 命名空间中的其他类型,如 MethodInfo 、 PropertyInfo 、 MemberInfo 和 ConstructorInfo 。
以下是一些示例代码,展示了如何获取常见类型的相关名称:
let intType = typeof<int>
printfn "intType.FullName: %s" intType.FullName
printfn "intType.AssemblyQualifiedName: %s" intType.AssemblyQualifiedName
let intListType = typeof<int list>
printfn "intListType.FullName
超级会员免费看
订阅专栏 解锁全文
1101

被折叠的 条评论
为什么被折叠?



