在列表中的情況下,你可以使用System.Collections.IList
訪問元素
open System.Collections
open System.Collections.Generic
let x = List<int>()
let y: IList = downcast (x :> obj)
這種方法也可以推廣:讓您的泛型類實現非泛型接口:
type IT =
abstract Value: obj
type T<'a>(a: 'a) =
member val Value = a;
interface IT with
member this.Value = upcast this.Value
如果不是一個選項(即,因爲你不能在課堂上的變化),你總是可以訴諸反射
type T<'a>(a: 'a) =
member val Value = a;
type Action =
static member Do(a: T<_>) = printfn "%A" a.Value
let v = T(10)
let mi = typeof<Action>.GetMethod("Do").MakeGenericMethod(v.GetType().GetGenericArguments().[0])
mi.Invoke(null, [|v|])
你能澄清一下嗎 - 你有一個已知的非對象類型的列表嗎(在你的例子中是'int'),還是你有一個你知道的對象是一些未知類型的列表? – kvb 2014-10-02 18:24:19