Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Generics f#泛型类型比较_Generics_F#_Types - Fatal编程技术网

Generics f#泛型类型比较

Generics f#泛型类型比较,generics,f#,types,Generics,F#,Types,我试图弄清楚从一个调用返回的obj是否属于某种类型。这是我的密码: type MyType<'T>= val mutable myArr : array val mutable id : int val mutable value : 'T 我如何判断A型是MyType? < P>我不认为这很简单(请记住我是天真的)考虑 match a with | :? MyType<int> as mt -> // it's a MyType<

我试图弄清楚从一个调用返回的obj是否属于某种类型。这是我的密码:

type MyType<'T>= 
    val mutable myArr : array
    val mutable id : int
    val mutable value : 'T

我如何判断A型是MyType?

< P>我不认为这很简单(请记住我是天真的)考虑

match a with
| :? MyType<int> as mt -> // it's a MyType<int>, use 'mt'
| _ -> // it's not
1) 我们在多种类型上使用泛型 2) 我们没有对象的类型信息,所以它作为类型obj进入函数,就像在一些.NET datacontract/序列化库中一样

我修改了我的提案,使用了反思:

type SomeType<'A> = { 
        item : 'A 
    } 


type AnotherType<'A> = { 
    someList : 'A list 
} 

let test() = 

    let getIt() : obj =  
        let results : SomeType<AnotherType<int>> = { item = { someList = [1;2;3] }} 
        upcast results 

    let doSomething (results : obj) =  
        let resultsType = results.GetType()
        if resultsType.GetGenericTypeDefinition() = typedefof<SomeType<_>> then 
            let method = resultsType.GetMethod("get_item")
            if method <> null then
                let arr = method.Invoke(results, [||]) 
                if arr.GetType().GetGenericTypeDefinition() = typedefof<AnotherType<_>> then 
                    printfn "match" 

    getIt() |> doSomething  
键入SomeType doSomething

似乎应该有更自然的方法来做这件事……

看来我还有另一个问题。似乎有些FuntReturnsObj()返回“U”。使用您建议的第一个方法,我得到:“从类型“U”到MyType的此运行时强制或类型测试涉及基于此程序点之前的信息的不确定类型。不允许对某些类型进行运行时类型测试。需要进一步的类型注释。”然后,只需先调用“box”。“将框a与…”匹配我不确定您所说的“框”Brian是什么意思?调用
box
函数,该函数将其参数向上转换为type
obj
.hmm,运行时类型检查?你知道你的方法返回什么类型吗?如果是这样的话,也许您可以将类型包装在一个类型安全的联合中并使用模式匹配,也许您在该函数中的类型可以公开一个公共接口?有很多方法可以避免运行时类型检查。如果不知道返回的类型是什么呢?请看我下面的帖子,因为T1与T1不同,如果您只关心T1的对象,而不考虑其他细节,该怎么办?这非常有助于确定结果是否属于我想要的类型
let t = a.GetType()
if t.IsGenericType && t.GetGenericTypeDefinition() = typedefof<MyType<int>> then
    // it is
type SomeType<'A> = { 
        item : 'A 
    } 


type AnotherType<'A> = { 
    someList : 'A list 
} 

let test() = 

    let getIt() : obj =  
        let results : SomeType<AnotherType<int>> = { item = { someList = [1;2;3] }} 
        upcast results 

    let doSomething (results : obj) =  
        let resultsType = results.GetType()
        if resultsType.GetGenericTypeDefinition() = typedefof<SomeType<_>> then 
            let method = resultsType.GetMethod("get_item")
            if method <> null then
                let arr = method.Invoke(results, [||]) 
                if arr.GetType().GetGenericTypeDefinition() = typedefof<AnotherType<_>> then 
                    printfn "match" 

    getIt() |> doSomething