1
votes

I have a variable in fsharp interactive

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

(I got that value from a call to a regular dotnet library, whose prototype tells me it returns an obj )

I would like to access the value inside of it, but I don't know exactly the type. So I try to reflect on it :

>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 = [||];

It has a Get method, however, when I try to retrieve the element, I get an error.

>toto.Get(0,0);;

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

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

What is the correct way to retrieve the inside element ?

PS : casting it beforehand yields the same

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

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

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

same for .[0, 0]

> toto.[0, 0];;

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

error FS0039: The field, constructor or member 'Item' is not defined
2
The expression toto : obj = [[""]] shouldn`t even compile. - JaredPar
this is unfortunate consequence that list of lists and multidimensional arrays are printed in the similar fashion in FSI - desco
@JaredPar I failed to mention, because I thought the displayed information was sufficient, I get this value from a dotnet function call. whose return type, in all its glory, is object.... - nicolas

2 Answers

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

I imagine

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

is what you want.