Warm tip: This article is reproduced from serverfault.com, please click

reflection-在F#中,如何访问函数的属性?

(reflection - In F#, how can I access attributes on a function?)

发布于 2020-12-05 01:58:36

假设我正在F#中创建一个属性,并将其应用于函数,如下所示:

type MyAttribute(value: int) =
    inherit System.Attribute()
    member this.Value = value

[<My(42)>]
let myFunction() = ()

如何通过反射检索该属性?

理想情况下,我想使用一些类似的东西,myFunction.GetType().GetCustomAttributes(true)但这是行不通的。

Questioner
Dschroer
Viewed
1
Arshia001 2020-12-05 21:35:27

myFunction.GetType()之所以不起作用,是因为FSharpFunc<_,_>每次你引用一个函数时,F#编译器都会自动创建一个子类。你将获得的类型FSharpFunc,这不是你所需要的。

要获取函数的反射信息,你需要先找到该函数所在的模块。每个模块都被编译成一个静态类,你可以在该类中找到该函数。因此,要获得此功能:

module MyModule

let myFunc x = x + 1

你需要执行以下操作(我没有检查代码):

// Assuming the code is in the same assembly as the function;
// otherwise, you must find the assembly in which the module lives
let assm = System.Reflection.Assembly.GetExecutingAssembly()
let moduleType = assm.GetType("MyModule")
let func = moduleType.GetMethod("myFunc")
let attrib = func.GetCustomAttribute<MyAttribute>()