开发者

How can i obtain in C# a specific operator's function?

开发者 https://www.devze.com 2022-12-24 00:25 出处:网络
Is it possible to obtain the function behind a C# operator? For example in F# you can do let add = (+);;

Is it possible to obtain the function behind a C# operator? For example in F# you can do

let add = (+);;

val add : (int -&开发者_开发百科gt; int -> int)

Is it possible in c# to do something like this:

Func<int, int, int> add = (+);

or

Func<int, int, int> add = Int32.(+)

?


You can use lambda expressions:

Func<int, int, int> func = (val1, val2) => val1 + val2;


Here's an indirect way to do what you want:

Func<int, int, int> add = (int a, int b) => a + b;

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).


To add to the other answers, addition of ints is done by the CIL add instruction, rather than a method call, so there isn't any sense in which it would be possible to acquire 'the actual MethodInfo'.

0

精彩评论

暂无评论...
验证码 换一张
取 消