温馨提示:本文翻译自stackoverflow.com,查看原文请点击:f# - FSharp.Compiler.Service FSharpChecker fails to compile dynamic assembly when referencing IAsyncDispo
f#

f# - FSharp.Compiler.Service FSharpChecker在引用IAsyncDispo时无法编译动态程序集

发布于 2020-04-23 15:12:39

在dotnet core 3.1控制台应用程序中运行以下命令无法编译该程序集:

module Test

open FSharp.Compiler.SourceCodeServices
open FSharp.Compiler.Text

let source = """module Test

type TestType() = 
    interface System.IAsyncDisposable with
        member __.DisposeAsync() = 
            System.Threading.Tasks.ValueTask()
"""

let references = 
    [
        "Microsoft.Bcl.AsyncInterfaces"
    ]

[<EntryPoint>]
let main(args) =
    let checker = FSharpChecker.Create()

    let options = 
        { FSharpParsingOptions.Default with 
            SourceFiles = [|"empty.fs"|]
        }

    let sourceText = SourceText.ofString source   

    let parseResult = 
        checker.ParseFile("empty.fs", sourceText, options) |> Async.RunSynchronously

    let input = parseResult.ParseTree.Value

    let thisAssembly = 
        System.Reflection.Assembly.GetExecutingAssembly().GetName().Name

    let allReferences = 
        thisAssembly :: references 

    let errors, _, assemblyOpt =
        checker.CompileToDynamicAssembly([input], "TestProject", allReferences, None)
        |> Async.RunSynchronously

    0

assemblyOptNone,并且errors数组包含以下消息:

The type 'IAsyncDisposable' is not defined in 'System'. Maybe you want one of the following:
   IDisposable
The type 'obj' is not an interface type
The type 'IAsyncDisposable' is not defined in 'System'. Maybe you want one of the following:
   IDisposable
The type 'IAsyncDisposable' is not defined in 'System'. Maybe you want one of the following:
   IDisposable
This type is not an interface type
No abstract or interface member was found that corresponds to this override
The value, constructor, namespace or type 'ValueTask' is not defined.
p_tyar_spec: from error
p_tyar_spec: from error

但是,Microsoft.Bcl.AsyncInterfaces和System 都应包含IAsyncDisposable是什么导致缺少参考错误?

查看更多

提问者
Durdsoft
被浏览
53
7sharp9 2020-02-16 01:30

我怀疑它的程序集列表在某种程度上是不完整的,您可以通过将动态程序集生成功能更改为以下内容来确认这一点:

checker.CompileToDynamicAssembly([input], "TestProject", allReferences, None, noframework = true)

noframework = true

哪个有效,(我刚刚测试过)