2013-07-29 25 views
2

我有以下的C#類,我想利用在F#錯誤在F#腳本文件創建C#類的實例

using System; 
using System.Collections.Generic; 
using System.Text; 

namespace DataWrangler.Structures 
{ 
    public enum Type { Trade = 0, Ask = 1, Bid = 2 } 

    public class TickData 
    { 
     public string Security = String.Empty; 
     public uint SecurityID = 0; 
     public object SecurityObj = null; 
     public DateTime TimeStamp = DateTime.MinValue; 
     public Type Type; 
     public double Price = 0; 
     public uint Size = 0; 
     public Dictionary<string, string> Codes; 
    } 
} 

我想在F#創建它的一個實例。我使用的這個過程的代碼是在F#腳本文件

#r @"C:\Users\Chris\Documents\Visual Studio 2012\Projects\WranglerDataStructures\bin\Debug\WranglerDataStructures.dll" 

open System 
open System.Collections.Generic; 
open System.Text; 
open DataWrangler.Structures 

type tick = TickData // <- mouse over the "tick" gives me a tooltip with the class structure 

// it bombs out on this line 
let tickDataTest = tick(Security = "test", TimeStamp = DateTime(2013,7,1,0,0,0), Type = Type.Trade, Price = float 123, Size = uint32 10) 

我得到的錯誤是:

error FS0193: internal error: Could not load file or assembly 'file:///C:\Users\Chris\Documents\Visual Studio 2012\Projects\WranglerDataStructures\bin\Debug\WranglerDataStructures.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format. 

我檢查了文件路徑,他們似乎是正確的。我可以將鼠標懸停在'類型'上,它給了我C#對象的結構。所以它似乎在找到C#代碼。任何人都可以告訴我我在這裏做錯了什麼?句法?還是很新的C# - > F#introp

+0

我可以運行你的代碼沒有任何問題。也許你忘記了alt +進入F#交互的'#r'行? –

+0

沒有。它給了我正確的反饋,當我點擊該行:'Referenced'C:\ Users \ Chris \ Documents \ Visual Studio 2012 \ Projects \ WranglerDataStructures \ bin \ Debug \ WranglerDataStructures.dll'' –

+0

如何刪除'type tick'並使用'新的TickData'來創建對象實例? –

回答

7

有幾件事情在這裏檢查:

  1. 確保fsi.exe以位模式與您的WranglerDataStructures.dll兼容運行。通過在Visual Studio選項中的F#Tools - > F#Interactive - > 64-bit F#Interactive下設置一個標誌,可以將fsi.exe作爲64位或32位進程運行。您通常可以通過將C#程序集編譯爲任何CPU來避免這些類型的問題。

  2. 確保WranglerDataStructures.dll不依賴於其他您不從F#引用的庫。要麼在F#中添加引用,要麼將它們從WranglerDataStructures.dll中刪除。

如果這些步驟不能產生成功嘗試使用fuslogview.exe工具http://msdn.microsoft.com/en-us/library/e74a18c4.aspx,看看是不是被加載什麼參考。

+0

謝謝John!這確實是問題所在。我的C#dll是64x。只需要將FSI更改爲64x即可運行。 –