调试引用 F# lib 的 C# 应用程序时没有可用的源

发布于 2024-09-09 23:23:06 字数 620 浏览 4 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情场扛把子 2024-09-16 23:23:06

原则上,这应该可以在不设置任何其他选项的情况下工作。需要检查的一些事项:

  • 运行时能否找到 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?

与酒说心事 2024-09-16 23:23:06

该错误不是由任何 C#/F# 互操作问题引起的。
当您调用 b() 时,它会尝试访问 None 选项值的 Value,这会在运行时引发空引用异常。

在 F# 库中,尝试将 let aa:AA option = None 替换为 let aa:AA option = Some(AA())。然后,C# 代码应打印“xx”。

当向外界公开 F# 选项类型时,您可以允许空选项返回 null 值而不是异常。例如:

module Module1

//attribute needed in order to be able to return null from the match statement
[<AllowNullLiteralAttribute>] 
type AA() =
    let _a = "xx"
    member x.a = _a

let aa:AA option = None

let b() = match aa with
          | None -> null
          | Some value -> value 

并检查使用此 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 with let 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:

module Module1

//attribute needed in order to be able to return null from the match statement
[<AllowNullLiteralAttribute>] 
type AA() =
    let _a = "xx"
    member x.a = _a

let aa:AA option = None

let b() = match aa with
          | None -> null
          | Some value -> value 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文