gpt4 book ai didi

F# 编译代码在运行时以缺少程序集结束

转载 作者:行者123 更新时间:2023-12-01 10:02:16 32 4
gpt4 key购买 nike

我有一个与 this question 的答案中提供的代码相关的问题.

我遇到的问题是在运行时找不到传递给 CompilerParameters 的三个引用组件(System.dll、FSharp.Core.dll、FSharp.Powerpack.dll)。我得到的错误是:

unknown-file(0,0) : error 0: error FS0218: Unable to read assembly 'c:\user s\utente\documents\visual studio 2010\Projects\TrashSolution\TrashSolution\bin\D ebug\FSharp.Core.dll'

我如何告诉编译器在 GAC 中搜索这些程序集,而不是在项目的 bin 目录中?如果我在以字符串形式提供的代码中打开命名空间,我如何知道要添加哪些程序集?我在哪里可以获得这些信息?

最佳答案

在您链接的答案的代码中,底部有一行:

let asm = Reflection.Assembly.LoadFrom(fileinfo.Value.FullName)

如果您调用 Reflection.Load而是将完全限定程序集名称传递给它,它将尝试从 GAC(以及其他一些地方,如果程序集不在 GAC 中)加载程序集。

let asm =
Assembly.Load "SampleAssembly, Version=1.0.2004.0, Culture=neutral, PublicKeyToken=8744b20f8da049e3"

如果您不知道完全限定的程序集名称,您必须使用程序集的简单名称创建一个 AssemblyName,然后调用 Reflection.Load采用 AssemblyName 而不是 string 的重载。

let asmName = AssemblyName "Your.Assembly.Name"
let asm = Assembly.Load asmName

至于知道要加载哪些程序集——我认为没有一种简单的方法可以通过编程来确定它。我现在能想到的仅有的两个解决方案:

  1. 如果您对给定的代码(作为字符串)有一些知识,您可以使用 FSharpCodeProvider 解析它并查看哪些命名空间/模块已打开以及使用了哪些类型。如果您要查看是否使用了某些特定的命名空间或类型(即,您需要在编译代码时包含一个程序集引用),您可以创建一个映射(在您的 .fsx 正在将 namespace 和/或类型名称编译)为程序集名称,并使用它来引用适当的程序集。
  2. 您可以“暴力”搜索 GAC,方法是使用半文档化的 Fusion API 枚举 GAC 中安装的所有程序集,然后使用反射检查每个程序集并确定它是否是您需要的程序集。这可能非常慢,所以我会不惜一切代价避免它。如果您决定走这条路,您必须还使用Assembly.ReflectionOnlyLoad 方法来加载程序集!这允许程序集在您完成检查后被卸载——如果您使用普通反射,则程序集无法被卸载,您的程序可能会因 OutOfMemoryException 或类似错误而崩溃。

编辑: 结果表明,通过简单名称加载程序集在 fsi 中成功,但在普通 F# 代码中却失败,因为 fsi 会自动安装一个AppDomain.AssemblyResolve 的处理程序事件。当您尝试加载程序集但无法解析时,CLR 会触发此事件;该事件为您提供了一种“手动”解析程序集和/或动态生成程序集并返回它的方法。

如果您查看尝试在 F# 项目中运行代码时引发的 FileNotFoundException,您会在异常的 Fusion Log 属性中看到类似以下内容:

=== Pre-bind state information ===
LOG: User = Jack-Laptop\Jack
LOG: DisplayName = System
(Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: System | Domain ID: 1
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/Users/Jack/Documents/Visual Studio 2010/Projects/StackOverflow1/StackOverflow1/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : StackOverflow1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Users/Jack/Documents/Visual Studio 2010/Projects/StackOverflow1/StackOverflow1/bin/Debug/System.DLL.
LOG: Attempting download of new URL file:///C:/Users/Jack/Documents/Visual Studio 2010/Projects/StackOverflow1/StackOverflow1/bin/Debug/System/System.DLL.
LOG: Attempting download of new URL file:///C:/Users/Jack/Documents/Visual Studio 2010/Projects/StackOverflow1/StackOverflow1/bin/Debug/System.EXE.
LOG: Attempting download of new URL file:///C:/Users/Jack/Documents/Visual Studio 2010/Projects/StackOverflow1/StackOverflow1/bin/Debug/System/System.EXE.

查看该日志的底部,您会看到 CLR 在放弃之前搜索程序集的位置。

这是一个简单的处理程序,可让您了解如何使用 AppDomain.AssemblyResolve 处理程序手动解析程序集。 (注意:需要在尝试加载程序集的代码之前添加处理程序!)

System.AppDomain.CurrentDomain.add_AssemblyResolve (
System.ResolveEventHandler (fun _ args ->
let resolvedAssembly =
System.AppDomain.CurrentDomain.GetAssemblies ()
|> Array.tryFind (fun loadedAssembly ->
// If this assembly has the same name as the one we're looking for,
// assume it's correct and load it. NOTE : It may not be the _exact_
// assembly we're looking for -- then you'll need to adjust the critera below.
args.Name = loadedAssembly.FullName
|| args.Name = loadedAssembly.GetName().Name)

// Return null if the assembly couldn't be resolved.
defaultArg resolvedAssembly null))

如果您将该代码添加到新的 F# 控制台项目,然后添加使用 AssemblyName 和 Assembly.Load 的代码,您应该能够加载 System 程序集因为它在 F# 项目中默认引用,并且会在您运行项目时加载。如果您尝试解析 System.Drawing,它将失败,因为我们的自定义事件处理程序找不到程序集。显然,如果您需要一些更复杂的程序集解析逻辑,您应该以对您的应用程序有意义的任何方式将其构建到事件处理程序中。

最后,这是异常消息中提到的 MSDN 白皮书的链接:Best Practices for Assembly Loading .如果您遇到困难并且无法弄清楚如何解决所需的程序集,那么值得一读。

关于F# 编译代码在运行时以缺少程序集结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14578665/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com