- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想编写一个函数,它接受函数 f 作为参数并返回与 f 关联的 System.Reflection.MethodInfo。
不太确定是否可行。
最佳答案
所以,我终于找到了解决办法。非常hacky,但是嘿!有用! (编辑:仅在 Debug模式下)。
let Foo (f:S -> A[] -> B[] -> C[] -> D[] -> unit) =
let ty = f.GetType()
let argty = [|typeof<S>; typeof<A[]>; typeof<B[]>; typeof<C[]>;typeof<D[]>|]
let mi = ty.GetMethod("Invoke", argty)
let il = mi.GetMethodBody().GetILAsByteArray()
let offset = 9//mi.GetMethodBody().MaxStackSize
let token = System.BitConverter.ToInt32(il, offset)
let mb = ty.Module.ResolveMethod(token)
match Expr.TryGetReflectedDefinition mb with
| Some ex -> printfn "success %A" e
| None -> failwith "failed"
即使 f 是在另一个程序集 (.dll) 中或在调用 Foo 的同一程序集中定义的,它也能正常工作。它还不完全通用,因为我必须定义 argty 是什么,但我确信我可以编写一个函数来完成它。
事实证明,在编写此代码后,达斯汀对同一问题有类似的解决方案,尽管是用 C# 编写的(参见 here )。
编辑:这是一个使用示例:
open System
open Microsoft.FSharp.Quotations
[<ReflectedDefinition>]
let F (sv:int) (a:int[]) (b:int[]) (c:int[]) (d:int[]) =
let temp = a.[2] + b.[3]
c.[0] <- temp
()
let Foo (f:S -> A[] -> B[] -> C[] -> D[] -> unit) =
let ty = f.GetType()
let arr = ty.BaseType.GetGenericArguments()
let argty = Array.init (arr.Length-1) (fun i -> arr.[i])
let mi = ty.GetMethod("Invoke", argty)
let il = mi.GetMethodBody().GetILAsByteArray()
let offset = 9
let token = System.BitConverter.ToInt32(il, offset)
let mb = ty.Module.ResolveMethod(token)
mb
let main () =
let mb = Foo F
printfn "%s" mb.Name
match Expr.TryGetReflectedDefinition mb with
| None -> ()
| Some(e) -> printfn "%A" e
do main ()
它的作用是打印 F 的名称及其 AST(如果该函数是反射定义)。
但是经过进一步调查,这个 hack 恰好只在 Debug模式下有效(并且 F 必须是一个函数值以及顶级定义),所以不妨说它是一个 不可能做的事情。
以下是调试/发布版本中 FSharpFunc 的 Invoke 方法的 IL 代码:
Debug模式:
.method /*06000007*/ public strict virtual
instance class [FSharp.Core/*23000002*/]Microsoft.FSharp.Core.Unit/*01000006*/
Invoke(int32 sv,
int32[] a,
int32[] b,
int32[] c,
int32[] d) cil managed
// SIG: 20 05 12 19 08 1D 08 1D 08 1D 08 1D 08
{
// Method begins at RVA 0x21e4
// Code size 16 (0x10)
.maxstack 9
IL_0000: /* 00 | */ nop
IL_0001: /* 03 | */ ldarg.1
IL_0002: /* 04 | */ ldarg.2
IL_0003: /* 05 | */ ldarg.3
IL_0004: /* 0E | 04 */ ldarg.s c
IL_0006: /* 0E | 05 */ ldarg.s d
IL_0008: /* 28 | (06)000001 */ call void Program/*02000002*/::F(int32,
int32[],
int32[],
int32[],
int32[]) /* 06000001 */
IL_000d: /* 00 | */ nop
IL_000e: /* 14 | */ ldnull
IL_000f: /* 2A | */ ret
} // end of method mb@25::Invoke
Release模式:
method public strict virtual instance class [FSharp.Core]Microsoft.FSharp.Core.Unit
Invoke(int32 sv,
int32[] a,
int32[] b,
int32[] c,
int32[] d) cil managed
{
// Code size 28 (0x1c)
.maxstack 7
.locals init ([0] int32 V_0)
IL_0000: nop
IL_0001: ldarg.2
IL_0002: ldc.i4.2
IL_0003: ldelem [mscorlib]System.Int32
IL_0008: ldarg.3
IL_0009: ldc.i4.3
IL_000a: ldelem [mscorlib]System.Int32
IL_000f: add
IL_0010: stloc.0
IL_0011: ldarg.s c
IL_0013: ldc.i4.0
IL_0014: ldloc.0
IL_0015: stelem [mscorlib]System.Int32
IL_001a: ldnull
IL_001b: ret
} // end of method mb@25::Invoke
您可以看到,在 Release模式下,编译器将 F 的代码内联到 Invoke 方法中,因此调用 F 的信息(以及检索 token 的可能性)消失了。
关于reflection - 检索 F# 函数的 MethodInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1575180/
我有: func NewMethodDescriptor(typ interface{}) *MethodDescriptor { reflectedMethod := reflect.Val
我需要确定地检查 reflect.Type 是否是一个错误。 错误没有反射(reflect)类型。在 go reflect 中检查类型错误的正式/惯用方式是什么? Go Playground Full
根据 reflect 文档 reflect.Value.MapIndex() 应返回一个 reflect.Value,它表示存储在 map 特定键处的数据的值.所以我的理解是以下两个表达式应该是相同的
与 reflect pkg 有点混淆 所有示例都使用 reflect.NewValue() 来获取 var 的 reflect.Value,但是 func NewValue 未记录在 http://g
在计算机语言的上下文中,我从未找到关于反射的词源的明确解释,所以我想在这里澄清一下。 “Reflection”源于拉丁语,有以下definitions : bend back turn back tu
我写了一个漂亮的函数,它可以接受 system.object ,反射(reflect)其属性并将对象序列化为 JSON 字符串。它看起来像这样: public class JSONSerializer
我正在尝试创建一个函数 import Language.Reflection foo : Type -> TT 我尝试使用reflect 策略: foo = proof { intro t
最近我和一位同事谈论 C++,感叹没有办法获取带有类字段名称的字符串并提取具有该名称的字段;换句话说,它缺乏反射(reflection)。他困惑地看着我,并问什么时候有人需要做这样的事情。 除了“嘿,
我正在考虑允许模块与属性文件中的类一起使用的想法;像 availableModules.properties Contact=org.addressbook.ContactMain Business=
这个问题特别与为具有大量字段的对象覆盖 equals() 方法有关。首先,让我说这个大对象不能在不违反 OO 原则的情况下分解成多个组件,所以告诉我“没有类应该有超过 x 个字段”无济于事。 继续前进
例子 router.Get(path, handler) // works fine methodStr = "Get" router.methodStr(path, handler) // e
我一直坚持使用反射库的问题。由于很多推荐,我决定使用它,但我只是在学习,有些部分并不是很容易.. 我有这部分代码: func countDataByName(sourceName string, s
我有一个包含一些 url 参数的特定结构,我想使用 reflect 构建一个 url 参数字符串以遍历结构字段,这样我就不会关心结构真正包含什么。 假设我有一个这样的结构: type Student
我正在尝试从 reflect.Value 中检索字符串值, 我希望 value.String()成为okok但我得到了相反。 我错过了什么吗? package main import ( "f
为了避免创建 org.reflections.Reflections 类的多个实例,我只想创建一个并根据需要重用。有谁知道这个类是否是线程安全的? 如果它不是线程安全的,我知道我可以使用 Java 的
我最近对引用、具体化和反射(reflection)感到困惑。有人可以很好地解释他们的关系和差异(如果有的话)吗? 最佳答案 引用 这可能是最简单的一个。考虑一下当您在 REPL 中键入以下内容时会发生
less main.go输出: ``` package main import ( "reflect" "net/url" "fmt" ) type User struct {
我在 golang 中使用 gorm 包 ( https://github.com/jinzhu/gorm ) 作为我的数据库库。我有很多类(数据库表),如“酒店”或“套餐”。复制代码不是好的编程习惯
我有代码 var t reflect.Type = LaunchController(route.controller) // create controller ptr . var
是否有可能以及如何在不从类型创建对象并调用它的情况下获取类型的 reflect.Type reflect.TypeOf(obj) Java 中的内容是:MyType.class 最佳答案 您可以使用以
我是一名优秀的程序员,十分优秀!