为什么这两个都编译但只运行其中一个?

时间:2017-03-23 19:15:36

标签: f# com delegates

open System
open System.Diagnostics
open System.Runtime.InteropServices
module PInvoke = 
    type EnumThreadDelegate= delegate of (IntPtr * IntPtr) -> bool
    type ComArrayList() =
        inherit System.Collections.ArrayList() 

    [<DllImport("user32.dll")>]
    extern [<return: MarshalAs(UnmanagedType.Bool)>] bool private EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
    let getThreadWindows (threadId:int) : _ list = 
        let items = ResizeArray()
        let withData (hWnd:IntPtr, lParam:IntPtr) = 
            let _ = items.Add(hWnd,lParam)
            true
        let f = EnumThreadDelegate withData

        EnumThreadWindows (threadId, f, IntPtr.Zero) |> ignore<bool>
        items
        |> Seq.cast<IntPtr*IntPtr>
        |> List.ofSeq


let lp = Process.GetProcesses() |> Seq.filter(fun p -> p.ProcessName.StartsWith("L")) |> Seq.minBy(fun p -> p.StartTime) 
lp.Threads
|> Seq.cast<ProcessThread> 
|> Seq.map (fun t -> t.Id) 
|> Seq.map PInvoke.getThreadWindows
|> List.ofSeq

给出System.Runtime.InteropServices.MarshalDirectiveException: Cannot marshal 'parameter #1': Generic types cannot be marshaled

但是这个编译并运行:

open System
open System.Diagnostics
open System.Runtime.InteropServices
module PInvoke = 
    type EnumThreadDelegate= delegate of IntPtr * IntPtr -> bool
    type ComArrayList() =
        inherit System.Collections.ArrayList() 

    [<DllImport("user32.dll")>]
    extern [<return: MarshalAs(UnmanagedType.Bool)>] bool private EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
    let getThreadWindows (threadId:int) : _ list = 
        let items = ResizeArray()
        let withData (hWnd:IntPtr) (lParam:IntPtr) = 
            let _ = items.Add(hWnd,lParam)
            true
        let f = EnumThreadDelegate withData

        EnumThreadWindows (threadId, f, IntPtr.Zero) |> ignore<bool>
        items
        |> Seq.cast<IntPtr*IntPtr>
        |> List.ofSeq


let lp = Process.GetProcesses() |> Seq.filter(fun p -> p.ProcessName.StartsWith("L")) |> Seq.minBy(fun p -> p.StartTime) 
lp.Threads
|> Seq.cast<ProcessThread> 
|> Seq.map (fun t -> t.Id) 
|> Seq.map PInvoke.getThreadWindows
|> List.ofSeq

为什么两个都编译,但有一个例外?

delegate of (IntPtr*IntPtr) -> booldelegate of IntPtr*IntPtr->bool之间的区别是什么? 它们不应该是同一个东西吗? (IntPtr*IntPtr)IntPtr*IntPtr不一样吗?

1 个答案:

答案 0 :(得分:4)

我认为

delegate of (IntPtr*IntPtr) -> bool

是一个单参数委托,它采用Tuple<IntPtr,IntPtr>,而

delegate of IntPtr*IntPtr -> bool

是一个双参数委托,它将两个IntPtr作为它的两个参数。