I get data from a 3rd party API that just gives me back a System.Object, which I know to be a double[] under the covers. And to deal with that return type, I have found the code below to work wonderfully. However, I also get back some int[] arrays that are also masquerading as System.Object, specifically dates in the form YYYYMMDD (e.g. 20100310).
The conversion to float fails, and it just says that the specified cast is not valid. Does anyone out there know how to make this work for integers?
let oIsNull (obj : System.Object) = obj = null
let oIsArray (obj : System.Object) = obj.GetType().IsArray
let o2f (obj : System.Object) =
let mutable arr = [|Double.NaN|]
if (oIsNull obj = false) && (oIsArray obj = true) then
let objArr = obj :?> obj[]
let u = objArr.GetUpperBound(0)
let floatArr : float[] = Array.zeroCreate (u + 1);
for i in 0..u do
if objArr.[i] = null then
floatArr.[i] <- Double.NaN
else
let t = objArr.[i].GetType()
floatArr.[i] <- objArr.[i] :?> float
//else floatArr.[i] <- float objArr.[i]
arr <- floatArr
arr
Do you mean that you want
to do the object cast to int then promote to float?
Actually you would be better served by using pattern matching on types along the lines