3

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

这是我的测试代码:

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在引用中调用方法时,调用模式 getMethodInfo是在接口类型中声明的,它没有定义。那么我怎样才能得到实际的实现MethodInfo呢?然后我可以得到执行的报价吗?

4

1 回答 1

4

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

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

这不起作用,并且不限于接口或对象表达式:

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() @>

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

于 2014-06-02T14:39:52.610 回答