gpt4 book ai didi

c# - 从 C# 方法返回 F# 接口(interface)

转载 作者:太空狗 更新时间:2023-10-30 00:51:37 24 4
gpt4 key购买 nike

我正在将一些东西从 F# 重新编码为 C#,但遇到了一个问题。

在 F# 示例中,我有这样的内容:

let foo (x:'T) =
// stuff
{ new TestUtil.ITest<'T[], 'T[]> with
member this.Name input iters = "asdfs"
member this.Run input iters = run input iters
interface IDisposable with member this.Dispose() = () }

现在在我的 C# 版本中我有..

public class Derp
{
// stuff

public TestUtil.ITest<T, T> Foo<T>(T x)
{
// ???
// TestUtil.ITest is from an F# library
}
}

我如何在 C# 中重新创建 F# 功能?有没有办法在不完全重新定义 C# 中的 ITest 接口(interface)的情况下做到这一点?

最佳答案

C# 不支持定义这样的接口(interface)的匿名实现。或者,您可以声明一些内部类并返回它。示例:

public class Derp
{
class Test<T> : TestUtil.ITest<T, T>
{
public string Name(T[] input, T[] iters)
{
return "asdf";
}
public void Run(T[] input, T[] iters)
{
run(input, iters);
}
public void Dispose() {}
}

public TestUtil.ITest<T, T> Foo<T>(T x)
{
//stuff
return new Test<T>();
}
}

请注意,我不确定我是否正确获取了您的 F# 代码的类型,但这应该是一般的想法。

关于c# - 从 C# 方法返回 F# 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25977829/

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