从 C# 调用 F# 函数并获取空引用异常

发布于 2024-08-09 13:56:05 字数 1841 浏览 3 评论 0原文

我使用的是安装了 2009 年 10 月 F# CTP 的 Visual Studio 2008。

我正在尝试从我的 C# 程序中调用一些 F# 代码。大多数类型的 F# 函数似乎都可以工作,但有些函数未在 F# 中初始化并引发 NullReferenceException。执行此操作的是闭包和部分应用函数,即在 C# 中显示为 FastFunc<> 的函数。类型。

我是否做错或忘记了什么,或者这可能是 F# 或 .NET 的错误?

下面的代码是为了演示该问题。我实际上并没有尝试在实际应用程序中使用此代码。 而且,在 F# 中,一切正常。这是一个 F# 到 C# 问题

F#:

namespace FS      
module FunctionTypes =

    //these all work in c# as expected
    let Value = "value"

    let OneParam (str:string) = str

    let TwoParam (str:string) (str2:string) = str + " " + str2

    let Lambda =
        fun () -> "lambda"  


    //these functions are null references in C#
    // they do work as expected in F# interactive mode
    let PartialApplication = TwoParam "what's up"

    let Closure = 
        let o = new System.Object()
        fun (i:int) -> o.ToString() + i.ToString()

    let ClosureWrapper (i:int) =
        Closure i  

C#(参考 F# 项目和 FSharp.Core)

 //these work as expected:
        var oneParam = FS.FunctionTypes.OneParam("hey");
        var twoParam = FS.FunctionTypes.TwoParam("yeah", "you");
        var lambdaFunction = FS.FunctionTypes.Lambda();
        var value = FS.FunctionTypes.Value;
        //  in the May09 CTP, Value returned null, 
        //      so it must have been fixed in Oct09 CTP



 //these do not work--each throws a NullReferenceException.
        var partial = FS.FunctionTypes.PartialApplication.Invoke("hello");
        var closure = FS.FunctionTypes.Closure.Invoke(1);
        var closureWrapper = FS.FunctionTypes.ClosureWrapper(1);

 //  FS.FunctionTypes.Closure itself is null, 
 //  so is FS.FunctionTypes.PartialAppliction.
 //  FS.FunctionTypes.ClosureWrapper is a regular function, 
 //    but it calls Closure, which is null     

I'm using Visual Studio 2008 with the October 2009 F# CTP installed.

I'm trying to call some F# code from my C# program. Most types of F# functions seem to work, but some are not getting initialized in F# and are throwing NullReferenceExceptions. The ones doing this are closures and partially applied functions, i.e. things that appear in C# as FastFunc<> types.

Is there something I'm doing wrong or forgetting, or is this possibly a bug with F# or .NET?

the code below is to demo the problem. I'm not actually trying to use this code in a real application.
also, within F#, everything works correctly. this is an F#-to-C# problem

F#:

namespace FS      
module FunctionTypes =

    //these all work in c# as expected
    let Value = "value"

    let OneParam (str:string) = str

    let TwoParam (str:string) (str2:string) = str + " " + str2

    let Lambda =
        fun () -> "lambda"  


    //these functions are null references in C#
    // they do work as expected in F# interactive mode
    let PartialApplication = TwoParam "what's up"

    let Closure = 
        let o = new System.Object()
        fun (i:int) -> o.ToString() + i.ToString()

    let ClosureWrapper (i:int) =
        Closure i  

C# (references F# project and FSharp.Core)

 //these work as expected:
        var oneParam = FS.FunctionTypes.OneParam("hey");
        var twoParam = FS.FunctionTypes.TwoParam("yeah", "you");
        var lambdaFunction = FS.FunctionTypes.Lambda();
        var value = FS.FunctionTypes.Value;
        //  in the May09 CTP, Value returned null, 
        //      so it must have been fixed in Oct09 CTP



 //these do not work--each throws a NullReferenceException.
        var partial = FS.FunctionTypes.PartialApplication.Invoke("hello");
        var closure = FS.FunctionTypes.Closure.Invoke(1);
        var closureWrapper = FS.FunctionTypes.ClosureWrapper(1);

 //  FS.FunctionTypes.Closure itself is null, 
 //  so is FS.FunctionTypes.PartialAppliction.
 //  FS.FunctionTypes.ClosureWrapper is a regular function, 
 //    but it calls Closure, which is null     

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

把人绕傻吧 2024-08-16 13:56:05

它对我有用,我得到“what's up hello”,“System.Object1”,“System.Object1”用于部分,闭包和closureWrapper变量。您是否引用了良好的 FSharp.Core 程序集?

It works for me, i get "what's up hello", "System.Object1", "System.Object1" for partial, closure and closureWrapper vars. Are you referencing the good FSharp.Core assembly?

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