从函数中提取函数名 [英] Extract function name from a function

查看:146
本文介绍了从函数中提取函数名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能创建一个名为getFuncName函数,该函数类型的函数(单位 - >'一),并返回其名称



我是说的一个。 C#的开发者,他们说,你可以使用一个函数功能型物业。方法中所示的示例 这里



我试图将其转换为F#:



例如转换(单位 - >'一)一键入Func键< _>然后调用它的属性,但它总是返回字符串调用

 让getFuncName F = $。 b $ b让fFunc = System.Func< _>(FUN _  - > F())
fFunc.Method.Name

让customFunc()= 1.0

//返回调用,但我希望它回归customFunc
getFuncName customFunc

背景对该问题的一个位为:



我创建的类型的函数的数组(单位 - > Deedle.Frame)。我现在想通过这些功能调用循环他们和保存它们具有相同的名称作为函数的CSV名称为CSV。一些假设性的代码如下:

 让发电机:(单位 - >框架< INT,串>)数组= ... 

发电机
|> Array.iter(乐趣发电机 - 方式>发生器()SaveCsv(sprintf的%s\%s.csv__SOURCE_DIRECTORY__(getFuncName发生器)))

这是一个脚本感,而不是应用程序代码中使用。


解决方案

不知道你是如何搜索信息,但是搜索引擎的第一个查询给了我这样的响应:

 让getFuncName F = 
型让利'= f.GetType()
让方法'= type'.GetMethods()|> Array.find(乐趣米 - > m.Name =调用)。

让IL = method'.GetMethodBody()GetILAsByteArray()
让methodCodes = [字节OpCodes.Call .value的;字节OpCodes.Callvirt.Value]

让位置= IL |> Array.findIndex(好玩点¯x - > methodCodes |> List.exists((=)X))
让metadataToken = BitConverter.ToInt32(IL,位置+ 1)

让actualMethod = type'.Module.ResolveMethod metadataToken
actualMethod.Name




不幸的是,当F#编译器不内联函数体内转化为调用方法的代码才有效。




从的这里



虽然有可能是一个更简单的方法。


How can I create a function called getFuncName that takes a function of type (unit -> 'a) and returns its name.

I was talking to one of the C# devs and they said you could use the .Method property on a Func type as shown in an example here.

I tried to convert this to F# :

for example convert (unit -> 'a) to a type Func<_> then call the property on it but it always returns the string "Invoke".

let getFuncName f =
    let fFunc = System.Func<_>(fun _ -> f())
    fFunc.Method.Name

let customFunc() = 1.0

// Returns "Invoke" but I want it to return "customFunc"
getFuncName customFunc

A bit of background to this problem is:

I have created an array of functions of type (unit -> Deedle.Frame). I now want to cycle through those functions invoking them and saving them to csv with the csv name having the same name as the function. Some hypothetical code is below:

let generators : (unit -> Frame<int, string>) array = ...

generators
|> Array.iter (fun generator -> generator().SaveCsv(sprintf "%s\%s.csv" __SOURCE_DIRECTORY__ (getFuncName generator)))

This is being used in a scripting sense rather than as application code.

解决方案

Not sure how you searched for information, but the first query to the search engine gave me this response:

let getFuncName f =
   let type' = f.GetType()
   let method' = type'.GetMethods() |> Array.find (fun m -> m.Name="Invoke")

   let il = method'.GetMethodBody().GetILAsByteArray()
   let methodCodes = [byte OpCodes.Call.Value;byte OpCodes.Callvirt.Value]

   let position = il |> Array.findIndex(fun x -> methodCodes |> List.exists ((=)x))
   let metadataToken = BitConverter.ToInt32(il, position+1) 

   let actualMethod = type'.Module.ResolveMethod metadataToken
   actualMethod.Name

Unfortunately, this code only works when F# compiler does not inline function body into calling method.

Taken from here

Although there may be a more simple way.

这篇关于从函数中提取函数名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆