2014-09-10 86 views
0

任何任何從f#代碼調用c#函數的想法。從F調用C#

我有像這樣

public class VehicleDetailsBridge 
{ 

    public void SaveVehicle(long id) 
    { 
     var db = "test call"; 
    } 
} 

一個C#函數,那麼F#中,我有:

type Persistence as this = 
    member x.SaveToDatabase(LatestVid) 
    let b = VehicleDetailsBridge() 
    b.SaveVehicle(LatestVid) 

我在F#項目C#類庫的引用。

然而,當我運行這段代碼我得到以下例外Xamarin:

Mono.Debugger.Soft.VMDisconnectedExecption

+0

FWIW,鑑於帕特里克麥當勞提供的糾正語法,我不能在Visual Studio中重現此問題在Windows ...我成立一個C#庫項目,並從一個F#控制檯項目中引用它,並從'Main'中調用'Persistance'類。沒問題。很容易得出結論,這個問題涉及到一個特定的單聲道環境... – 2014-09-10 13:34:57

回答

0

試試這個:

let b = VehicleDetailsBridge() // create instance of class 
b.SaveVehicle(LatestVid) 

VehicleDetailsBridge是所以你需要打開這個課程。

+0

我的錯誤仍然無法正常工作 – user1853454 2014-09-10 10:05:52

2
type Persistence() =       // need braces here, and not 'as this' 
    member x.SaveToDatabase(latestVid) = 
     let b = VehicleDetailsBridge()  // indent the method body 
     b.SaveVehicle(latestVid) 

我的全F#程序如下:

open FsCsInterop 

type Persistence() =       // need braces here, and not 'as this' 
    member x.SaveToDatabase(latestVid) = 
     let b = VehicleDetailsBridge()  // indent the method body 
     b.SaveVehicle(latestVid) 
     printfn "Saved %d" latestVid 

[<EntryPoint>] 
let main argv = 
    let p = Persistence() 
    p.SaveToDatabase(2L) 
    0 

// Prints "Saved 2" 
+0

仍然同樣的錯誤,此刻你可以引用C#從f# – user1853454 2014-09-10 10:38:55

+0

破解當試圖實例化 – user1853454 2014-09-10 10:39:23

+0

我不能重現你的問題,現在在家裏,並在Mac上試用Xamarin,它工作正常 – 2014-09-10 20:00:28