chiamando le funzioni F # da C # e ottenendo eccezioni riferimento null
Domanda
Sto usando Visual Studio 2008 con il F # CTP di ottobre 2009 installato.
Sto provando a chiamare un codice F # dal mio programma C #. La maggior parte dei tipi di funzioni F # sembra funzionare, ma alcuni non vengono inizializzati in F # e generano NullReferenceExceptions. Quelli che fanno questo sono chiusure e funzioni parzialmente applicate, cioè cose che appaiono in C # come FastFunc & Lt; & Gt; tipi.
C'è qualcosa che sto facendo di sbagliato o di dimenticare, o è forse un bug con F # o .NET?
il codice seguente è per dimostrare il problema. In realtà non sto cercando di utilizzare questo codice in una vera applicazione. inoltre, all'interno di F #, tutto funziona correttamente. questo è un problema da F # -to-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 # (fa riferimento al progetto F # e 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
Soluzione
Funziona per me, ottengo " che succede ciao " ;, " System.Object1 " ;, " System.Object1 " ; per parziali, chiusure e chiusure Wrapper. Stai facendo riferimento al buon assemblaggio di FSharp.Core?