从 C# 调用 F# 函数并获取空引用异常
我使用的是安装了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它对我有用,我得到“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?