F#は動的呼び出し演算子をサポートしています。しかし、あなたはあなたを実装する必要があります。ここであなたが
よう
Tuple
なものとして渡す必要があるだろうのparamsのためとして
// Dynamically invoke 'Next' method of 'Random' type
let o = box (new Random())
let a : int = o?Next(10)
を次のようにそれを使用することができるだろうhttp://www.fssnip.net/2U/title/Dynamic-operator-using-Dynamic-Language-Runtime
// Reference C# implementation of dynamic operations
#r "Microsoft.CSharp.dll"
open System
open System.Runtime.CompilerServices
open Microsoft.CSharp.RuntimeBinder
// Simple implementation of ? operator that works for instance
// method calls that take a single argument and return some result
let (?) (inst:obj) name (arg:'T) : 'R =
// TODO: For efficient implementation, consider caching of call sites
// Create dynamic call site for converting result to type 'R
let convertSite =
CallSite<Func<CallSite, Object, 'R>>.Create
(Binder.Convert(CSharpBinderFlags.None, typeof<'R>, null))
// Create call site for performing call to method with the given
// name and a single parameter of type 'T
let callSite =
CallSite<Func<CallSite, Object, 'T, Object>>.Create
(Binder.InvokeMember
(CSharpBinderFlags.None, name, null, null,
[| CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null);
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) |]))
// Run the method call using second call site and then
// convert the result to the specified type using first call site
convertSite.Target.Invoke
(convertSite, callSite.Target.Invoke(callSite, inst, arg))
から採取されたサンプルの実装であります
target?method(param1, param2)
これは、ターゲットメソッドがその引数をタプルとして処理することを意味するため、一部のパターンマッチングが関与する場合とされない場合があります。
出典
2017-08-12 19:17:15
Ody
もっと現実的なユースケースを教えてください。動的に処理する方法はいくつかありますが、エラーを導入するのが簡単になるため、可能な限り回避するのが一般的です。あなたの特定の例では、静的に知られている型を持つ 'func'関数があるので、動的に呼び出すことは役に立たないようです。 – kvb
この場合、私はラップされた関数を呼び出すことで引数を収集し、実際に関数を呼び出すリモートマシンにそれらを送信するリモート呼び出しについて考えていました。 –