3

有没有办法从代码引用中提取文件名和行号,以便我们可以添加添加调试信息以转换引用的代码。有什么替代方法吗?

4

2 回答 2

3

尝试这样的事情:

let sourceInfo (e:Quotations.Expr) = 
    let (|Val|_|) e : 't option = 
        match e with
        | Quotations.Patterns.Value(:? 't as v,_) -> Some v
        | _ -> None
    let (|Tup|_|) = Quotations.Patterns.(|NewTuple|_|)

    e.CustomAttributes
    |> List.tryPick (function | Tup [Val("DebugRange")
                                     Tup [Val(file:string)
                                          Val(startLine:int) 
                                          Val(startCol:int)
                                          Val(endLine:int)
                                          Val(endCol:int)]] 
                                  -> Some(file,startLine,startCol,endLine,endCol) 
                              | _ -> None)

sourceInfo <@ "test" @>
于 2012-08-02T14:29:11.897 回答
0

这现在内置在 F# 中

https://msdn.microsoft.com/en-us/library/dd233234.aspx

__LINE__
__SOURCE_DIRECTORY__
__SOURCE_FILE__
于 2015-02-26T19:45:46.367 回答