gpt4 book ai didi

c# - 从 C# 调用带有嵌入空格 (`` XXX YY`` 的 F# 勾选函数名称

转载 作者:可可西里 更新时间:2023-11-01 08:55:43 25 4
gpt4 key购买 nike

在 F# 中,我们可以创建这样的函数:

let ``add x and y`` x y = x + y

我通常可以这样调用它:

``add x and y`` 1 2

有没有办法从 C# 端调用上面的函数?不过,我什至无法在对象浏览器中看到它。

最佳答案

您可以公开任何 valid F# 函数名称到 C# 作为任何 C# 有效函数名称使用 CompiledName属性:

namespace Library1
module Test =
[<CompiledName("Whatever")>]
let ``add a and b`` x y = x + y

然后在 C# 中:

 using Library1;
...............
System.Console.WriteLine(Test.Whatever(2,2));

跟进 03/05/2016 来自 NickL 的评论, 至少适用于 F#3.1:

从函数转移到成员会带来一些“如果和但是”。

首先,如果从纯命名空间 使用,CompiledName 属性不会与member 一起编译。单纯的编译需要在 module 中使用。

当在 module 中使用并修饰 F# record 的 method member 时,无论两个刻度之间的内容看起来如何,它都可以正常工作.但是,当装饰 property member 时,F# 记录 CompiledName 仅当双刻度之间的内容类似于some 时才可见交叉汇编合法值名称:

module M

type MyRecord =
{ myField: string }
[<CompiledName "Whatever">]
member x.``Blah Blah blah``() = x.myField
[<CompiledName "Another">]
member x.``ABC`` = x.myField

然后在 C# 中以下工作正常:

var recInC = new M.MyRecord("Testing...");
Console.WriteLine(recInC.Whatever());
Console.WriteLine(recInC.Another);

这种不一致提示潜在的 issues .

关于c# - 从 C# 调用带有嵌入空格 (`` XXX YY`` 的 F# 勾选函数名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19327506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com