f#获取带约束的类型的泛型类型定义

时间:2012-02-01 20:40:19

标签: generics f#

这是最终目标:通过名称factory.CreateConnection<SqlConnection>("my db")

从工厂获取新连接

到目前为止,这是我的代码:

type IConnectionFactory =
    abstract member CreateConnection : unit -> IDbConnection

type DefaultConnectionFactory<'t when 't : (new : unit -> 't) and 't :> IDbConnection> () =
    member this.CreateConnection () = new 't()

    interface IConnectionFactory with
        member this.CreateConnection () = this.CreateConnection() :> IDbConnection

type DefaultConnectionFactoryFactory() =
    member this.CreateConnectionFactory connectionType =
        if not (typeof<IDbConnection>.IsAssignableFrom connectionType)
        then invalidArg "connectionType" (sprintf "type %O is not assignable to IDbConnection" connectionType)

        let genericFactoryType = typedefof<DefaultConnectionFactory<_>>
        let factorytype = genericFactoryType.MakeGenericType([| connectionType |])
        Activator.CreateInstance(factorytype) :?> IConnectionFactory

type ConnectionFactory (connectionStrings : Dictionary<string, (IConnectionFactory * string)>) =
    let ConnectionStrings = connectionStrings

    member this.CreateConnection connectionStringName : 't :> IDbConnection =
        if ConnectionStrings.ContainsKey connectionStringName
        then
            let connectionFactory, connectionString = ConnectionStrings.Item connectionStringName
            let connection = connectionFactory.CreateConnection ()
            connection.ConnectionString <- connectionString
            connection :?> 't
        else invalidArg "dataSourceName" (sprintf "no data source found named %s" connectionStringName)

在这一行:

let genericFactoryType = typedefof<DefaultConnectionFactory<_>>

我收到此错误: 对类型推断变量应用默认类型“IDbConnection”时,键入约束不匹配。泛型构造要求类型'IDbConnection'具有公共默认构造函数考虑添加其他类型约束

我不知道如何解决它......还欢迎任何其他建议

1 个答案:

答案 0 :(得分:3)

这与this question有关。问题是typedefof<DefaultConnectionFactory<_>>DefaultConnectionFactory上的约束需要一个实现IDbConnection的类型,并且具有默认构造函数。编译器知道没有这种类型。所以,你必须提供一个。您可以使用SqlConnection,例如:

typedefof<DefaultConnectionFactory<System.Data.SqlClient.SqlConnection>>

效果是一样的。 typedefof将返回无界类型DefaultConnectionFactory<>(使用C#术语),这在F#中无法表达。