如何在 C# 中获取特定运算符的函数?
是否可以获得C#运算符背后的函数? 例如,在 F# 中,您可以执行
let add = (+);;
val add : (int -> int -> int)
Is it possible in C# to do this:
Func<int, int, int> add = (+);
or
Func<int, int, int> add = Int32.(+)
?
Is it possible to obtain the function behind a C# operator?
For example in F# you can do
let add = (+);;
val add : (int -> int -> int)
Is it possible in c# to do something like this:
Func<int, int, int> add = (+);
or
Func<int, int, int> add = Int32.(+)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 lambda 表达式:
You can use lambda expressions:
这是一种完成您想要的操作的间接方法:
反映后,可以看到 F# 也不直接访问
Int32.operator+
(这可能是不可能的),而是执行类似于上面的 C# 代码的操作只是使用内部委托类型(Microsoft.FSharp.Core.FastFunc
)。Here's an indirect way to do what you want:
When reflected, it can be seen that F# doesn't access the
Int32.operator+
directly either (that's probably impossible), but does something like the C# code above just using an internal delegate type instead (Microsoft.FSharp.Core.FastFunc
).要添加到其他答案,
int
的添加是由 CILadd
指令完成的,而不是方法调用,因此没有任何意义可以获取“实际的MethodInfo
”。To add to the other answers, addition of
int
s is done by the CILadd
instruction, rather than a method call, so there isn't any sense in which it would be possible to acquire 'the actualMethodInfo
'.