2012-03-09 75 views
1

我有一個變量fsharp互動Fsharp互動福

val toto : obj = [["NKY INDEX"]] 

(我接到了一個電話到正規的dotnet庫值,其原型告訴我,它返回一個OBJ

我想要訪問它裏面的值,但我不知道類型。 所以我儘量體現它:

>toto.GetType();; 
val it : Type = 
    System.Object[,] 
    {Assembly = mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089; 
    AssemblyQualifiedName = "System.Object[,], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"; 
    Attributes = AutoLayout, AnsiClass, Class, Public, Sealed, Serializable; 
    BaseType = System.Array; 
    ContainsGenericParameters = false; 
    CustomAttributes = seq [[System.SerializableAttribute()]]; 
    DeclaredConstructors = [|Void .ctor(Int32, Int32); 
           Void .ctor(Int32, Int32, Int32, Int32)|]; 
    DeclaredEvents = [||]; 
    DeclaredFields = [||]; 
    DeclaredMembers = [|Void Set(Int32, Int32, System.Object); 
         System.Object& Address(Int32, Int32); 
         System.Object Get(Int32, Int32); 
         Void .ctor(Int32, Int32); 
         Void .ctor(Int32, Int32, Int32, Int32)|]; 
    DeclaredMethods = [|Void Set(Int32, Int32, System.Object); 
         System.Object& Address(Int32, Int32); 
         System.Object Get(Int32, Int32)|]; 
    DeclaredNestedTypes = seq []; 
    DeclaredProperties = [||]; 

它有一個Get方法,然而,當我嘗試檢索元素,我得到一個錯誤。

>toto.Get(0,0);; 

    toto.Get(0,0);; 
    -----^^^ 

    error FS0039: The field, constructor or member 'Get' is not defined 

什麼是檢索內部元素的正確方法?

PS:鑄造事前得到相同的

>(toto :?> System.Object[,]).Get(0,0);; 

(toto :?> System.Object[,]).Get(0,0);; 
----------------------------^^^ 

error FS0039: The field, constructor or member 'Get' is not defined 

同爲[0,0]

> toto.[0, 0];; 

    toto.[0, 0];; 
    ^^^^^^^^^^^ 

error FS0039: The field, constructor or member 'Item' is not defined 
+0

表達式'toto:obj = [[「」]]'不應該編譯。 – JaredPar 2012-03-09 18:38:44

+0

這是不幸的後果列表和多維數組的列表以FSI – desco 2012-03-09 18:41:50

+0

@JaredPar類似的方式打印我沒有提及,因爲我認爲顯示的信息是足夠的,我從一個dotnet函數調用得到這個值。他的所有榮耀中的歸來類型是對象...... – nicolas 2012-03-09 20:02:40

回答

1

我想象

let arr = toto :?> obj[,] // downcast to actual type 
let item = arr.[0,0] 

是你想要的。

+0

雖然有些東西似乎不對。原始類型應該是「字符串列表」正確的?通過'val toto:obj = [[「」]]'樣本。這也似乎不應該沒有明確的演員編譯。 – JaredPar 2012-03-09 18:46:01

+0

嘗試從我對FSI的回答中輸入代碼。原始對象是盒裝字符串[,] – desco 2012-03-09 18:51:16

+1

還有一個輸入錯誤;第二託託應該是arr。或者一次去:let item =(toto:?> obj [,])。[0,0] – Dirk 2012-03-09 19:33:29

2
let toto = box (Array2D.init 1 1 (fun _ _ -> "NKY INDEX")) 
(toto :?> string[,]).[0,0] 
+0

他有一個'obj [,]',是嗎?就好像lambda裏面的字符串字面也是盒裝的? – Brian 2012-03-09 21:38:08