引用と反映されたメソッド定義に関する API はかなり変更されています (そして、私は非常に初期の頃にいくつかのブログ投稿を書きました)。そのため、これは私のブログの最も古い部分です。ごめんなさい!
とにかく、次の単純なスニペットは現在の API を示しています。
[<ReflectedDefinition>]
let foo a b = a + b
open Microsoft.FSharp.Quotations
match <@@ foo 1 2 @@> with
// Matches a call to a static method that is marked as `ReflectedDefinition`
| Patterns.Call(None, DerivedPatterns.MethodWithReflectedDefinition body, args) ->
// Return the actual quotation - simply call the body that represents
// the function with all the arguments as arguments.
Expr.Applications(body, args |> List.map (fun a -> [a]))
| e -> e
大量の引用処理を行っている場合は、 と のopen
両方Patterns
をDerivedPatterns
実行してコードを短くすることをお勧めします (ただし、「ドット」を使用すると、モジュールを簡単に調べることができます)。
ここで重要なのMethodWithReflectedDefinition
は、どのパターンにも一致しMethodInfo
(プロパティにも同様のパターンがあります)、属性を持つメソッドの場合は引用符を返すパターンReflectedDefinition
です。関数を含むモジュールに属性を配置することもできます。