调试引用 F# lib 的 C# 应用程序时没有可用的源
在我的 VS2010 解决方案中,C# 应用程序项目引用了 F# 库项目。
当从 F# lib 抛出 NullReferenceException 时,调试器无法找到抛出的点异常。它只是说“没有可用的来源”。
我应该更改一些选项还是这是 VS2010 的限制?
我添加了一些示例代码:
F# 项目 'Library1'
module Module1
type AA() =
let _a = "xx"
member x.a = _a
let aa:AA option = None
let b() =
aa.Value.a // null reference occurs here
C# 项目 'ConsoleApp1'
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.Out.Write(Module1.b());
Console.In.Read();
}
}
}
In my solution of VS2010, a C# app project references a F# library project.
When a NullReferenceException
is thrown from F# lib, the debugger cannot find the point exception thrown. It just says 'No Source Available'.
Should I change some options or this is a limitation of VS2010?
I added some example code:
F# project 'Library1'
module Module1
type AA() =
let _a = "xx"
member x.a = _a
let aa:AA option = None
let b() =
aa.Value.a // null reference occurs here
C# project 'ConsoleApp1'
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.Out.Write(Module1.b());
Console.In.Read();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
原则上,这应该可以在不设置任何其他选项的情况下工作。需要检查的一些事项:
运行时能否找到 F# 库的 PDB 文件?
如果 PDB 文件与 DLL 位于同一目录中,则应该这样做。您还可以查看“输出”窗口并从组合中选择“调试”以查看应用程序启动时打印的信息 - 在那里您可以查看符号是否已加载。如果你的PDB符号在某个特殊的目录下,你可以在工具->目录中指定该目录。选项->调试->符号。
尝试在 F# 源代码中设置断点时会发生什么?
它是否显示“符号未加载”之类的内容(并以灰色显示断点)?它是否显示了任何可用于查找问题原因的附加信息?
This should, in principle, work without seting any additional options. A few things to check:
Can the runtime locate the PDB file for your F# library?
It should do that if the PDB file is in the same directory as the DLL. You can also look at the "Output" window and select "Debug" from the combo to see information printed when the application started - there you can see whether the symbols were loaded or not. If you have PDB symbols in some special directory, you can specify the directory in Tools -> Options -> Debugging -> Symbols.
What happens when you try to set a breakpoint in the F# source?
Does it say something like "symbols not loaded" (and show the breakpoint in gray color)? Does it shown any additional information that could be used to find the cause of the problem?
该错误不是由任何 C#/F# 互操作问题引起的。
当您调用
b()
时,它会尝试访问 None 选项值的 Value,这会在运行时引发空引用异常。在 F# 库中,尝试将
let aa:AA option = None
替换为let aa:AA option = Some(AA())
。然后,C# 代码应打印“xx”。当向外界公开 F# 选项类型时,您可以允许空选项返回 null 值而不是异常。例如:
并检查使用此 F# 库的任何代码中是否存在 null。
请注意
AllowNullLiteral
属性。尝试注释掉此属性,您将看到 F# 无法识别第一个匹配分支中的 null 值。The error isn't caused by any C#/F# interop issues.
When you call
b()
, it tries to access the Value of a None option value, which throws a null reference exception at runtime.In the F# lib, try replacing
let aa:AA option = None
withlet aa:AA option = Some(AA())
. The C# code should then print 'xx'.When exposing an F# option type to the outside world, you could allow a null return value from an empty option instead of an exception. For example:
And check for null in any code that uses this F# library.
Note the
AllowNullLiteral
attribute. Try commenting out this attribute and you'll see that F# won't recognize the value null in the first match branch.