f# - 如何获取接口(interface)实例方法的 F# 引用?

标签 f# quotations

在 F# 中,我们可以通过对象表达式创建接口(interface)实例,但是当我尝试在实例方法上使用属性 ReflectedDefinition 时,我无法获取引号。方法信息是在接口(interface)类型中声明的,而不是实例类型。

这是我的测试代码:

module Test

open System
open System.Reflection
open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Quotations.Patterns
open Microsoft.FSharp.Quotations.DerivedPatterns
open Microsoft.FSharp.Quotations.ExprShape

type IMyInterface =
    abstract Foo : int -> int

let createMyInterface () =
    { new IMyInterface with
        [<ReflectedDefinition>]
        member this.Foo a = a + 1 }

let expr =
    let a = createMyInterface()
    <@ a.Foo(42) @>

let rec iterExpr (expr:Expr) =
    match expr with
    | Call(objectExpr, info, paramExprs) ->
        printfn "info: %A" info
        printfn "reflected type: %A" info.ReflectedType
        match info with
        | MethodWithReflectedDefinition methodExpr ->
            printfn "%A" methodExpr
        | _ -> failwith "No reflected definition"

    | ShapeVar _ -> failwithf "TODO: %A" expr
    | ShapeLambda _ -> failwithf "TODO: %A" expr
    | ShapeCombination _ -> failwithf "TODO: %A" expr

let test() =
    iterExpr expr

[<EntryPoint>]
let main argv = 
    test()
    0 // return an integer exit code

如果我运行它,我得到异常:

C:\Users\Xiang\Documents\Inbox\TTTT\bin\Debug>TTTT
info: Int32 Foo(Int32)
reflected type: Test+IMyInterface

Unhandled Exception: System.Exception: No reflected definition
   at Microsoft.FSharp.Core.Operators.FailWith[T](String message)
   at Test.iterExpr(FSharpExpr expr) in C:\Users\Xiang\Documents\Inbox\TTTT\Program.fs:line 30
   at Test.test() in C:\Users\Xiang\Documents\Inbox\TTTT\Program.fs:line 37
   at Test.main(String[] argv) in C:\Users\Xiang\Documents\Inbox\TTTT\Program.fs:line 41

而且我还用 dotPeek 检查了生成的程序集,它是作为派生类实现的:

[CompilationMapping(SourceConstructFlags.ObjectType)]
  [Serializable]
  public interface IMyInterface
  {
    int Foo([In] int obj0);
  }

  [CompilationMapping(SourceConstructFlags.Closure)]
  [Serializable]
  [SpecialName]
  [StructLayout(LayoutKind.Auto, CharSet = CharSet.Auto)]
  internal sealed class createMyInterface\u004014 : Test.IMyInterface
  {
    public createMyInterface\u004014()
    {
      base.\u002Ector();
      Test.createMyInterface\u004014 createMyInterface14 = this;
    }

    [ReflectedDefinition]
    int Test.IMyInterface.Test\u002DIMyInterface\u002DFoo([In] int obj0)
    {
      return obj0 + 1;
    }
  }

所以,问题是,当我在引用中调用 Foo 方法时,调用模式获取 MethodInfo,它是在没有定义的接口(interface)类型中声明的。那么我怎样才能得到实际的实现 MethodInfo 呢?然后我可以获得实现报价?

最佳答案

简而言之,这是您的问题:

  1. 您正在通过定义方法的类型的实例调用虚拟方法。
  2. 您希望引用包含对派生类上定义的方法的调用。

这行不通,而且不限于接口(interface)或对象表达式:

type A() = 
    abstract M : unit -> unit
    default this.M() = printfn "abstract"

type T() =
    inherit A() with
        [<ReflectedDefinition>]
        override this.M() = printfn "override"

let expr =
    let a : A = upcast T()
    <@ a.M() @>

从根本上说,对象表达式的全部意义在于提供非密封类的匿名实现,所以你要求的对我来说没有意义——编译器只知道对象是某个实例实现该接口(interface)但无法知道实例的具体类型,因此无法知道使用哪个(可能有很多)具体方法。

关于f# - 如何获取接口(interface)实例方法的 F# 引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23989197/

相关文章:

asynchronous - 使用 LIFO 逻辑运行的 MailboxProcessor

f# - 将此 F# 代码重构为尾递归

linux - Linux 中的 .NET Core SDK 2.2.108 是否不支持 F# 匿名记录?

f# - 我可以获得 F# 引用的标识吗?

regex - 抓包组号

f# - 带参数的 NancyFx F# 应用程序

data-binding - F#:使用 INotifyPropertyChanged 进行数据绑定(bind)

c++ - 如何在 C++ 中打印\"

f# - 区分带引号的泛型值 "call"和泛型函数调用

javascript - 为什么我在其中输入内容后我的引文就消失了?