F# 中的 Uri().AbsolutePath "Unexpected symbol '.' in binding." 错误表达式

Uri().AbsolutePath "Unexpected symbol '.' in binding." error expression in F#

我在C#中有这样的语句:

    private static string LogPath
    {
        get
        {
            string filePath = new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath;
            return Path.GetDirectoryName(filePath) + "/cube_solver_log.txt";
        }
    }

当我尝试用 F# 编写它时;

static member LogPath() =
    let filePath = new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath
    Path.GetDirectoryName(filePath) + "/cube_solver_log.txt"

我遇到异常:

Unexpected symbol '.' in binding. Expected incomplete structured construct at or before this point or other token.

因为在 F# 中,我不知道为什么,系统库不接受我的代码中的 .AbsolutePath

我该如何解决这个问题?

您需要在 new 表达式两边添加方括号:

static member LogPath() =
    let filePath = (new Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath
    Path.GetDirectoryName(filePath) + "/cube_solver_log.txt"

事实上,new 关键字是可选的,因此您可以:

let  LogPath() =
    let filePath = Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath
    Path.GetDirectoryName(filePath) + "/cube_solver_log.txt"