在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
assemblyOpt
是None
,并且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
。是什么导致缺少参考错误?
我怀疑它的程序集列表在某种程度上是不完整的,您可以通过将动态程序集生成功能更改为以下内容来确认这一点:
checker.CompileToDynamicAssembly([input], "TestProject", allReferences, None, noframework = true)
noframework = true
哪个有效,(我刚刚测试过)
是的,就是这样。添加额外的引用后,它可以工作了。感谢您的宝贵时间,以及Twitter帮助!