【问题标题】:F# Interface ImplementationF# 接口实现
【发布时间】:2013-10-22 07:54:55
【问题描述】:

假设我在 C# 中有这个接口并想在 F# 中实现它

public interface IBatch
{
    System.Data.IDbConnection Connection { get; set; }
}

我希望在 F# 中实现接口,但无法找出正确的语法。我有这样的事情:

type public Batch = 
    interface IBatch with
        member f.Connection 
            with get() = new Devart.Data.Oracle.OracleConnection()
            and set value = ()

我得到的错误是:

此表达式的类型应为 IDbConnection,但此处的类型为 Devart.Data.Oracle.OracleConnection

【问题讨论】:

    标签: interface f#


    【解决方案1】:

    F# 不像 C# 那样实现隐式向下转换,你需要有

    type public Batch = 
        interface IBatch with
            member f.Connection 
                with get() = new Devart.Data.Oracle.OracleConnection() :> System.Data.IDbConnection
                and set value = ()
    

    【讨论】:

    • 啊。我知道这是我缺少的一个简单的语法细节。谢谢!将在 10 分钟内接受您的答复
    猜你喜欢
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    相关资源
    最近更新 更多