从F#生成类型提供程序提供的类型继承

时间:2018-06-26 01:49:35

标签: inheritance f# type-providers

我有这个基本的F#类型生成器

[<TypeProvider>]
type MyTypeProvider(config : TypeProviderConfig) as this = 
    inherit TypeProviderForNamespaces(config)

    let ns = "MyNamespace"
    let asm = Assembly.LoadFrom(config.RuntimeAssembly)

    let buildTypes (typeName:string) (args:obj[]) =
        let asm = ProvidedAssembly()
        let srvName = args.[0] :?> string
        ... omitted
        let provided = ProvidedTypeDefinition(asm, ns, typeName, Some typeof<MyRuntimeType>, hideObjectMethods = true, nonNullable = true, isErased = false)
        let ctor = ProvidedConstructor([], (fun _ -> <@@ MyRuntimeType() @@>))
        provided.AddMember(ctor)
        provided
    let parameters = 
        [ ProvidedStaticParameter("Host", typeof<string>, "") ]

    let provider = ProvidedTypeDefinition(asm, ns, "MyProvider", Some typeof<obj>, hideObjectMethods = true, nonNullable = true, isErased = false)
    do provider.DefineStaticParameters(parameters, buildTypes)
    do this.AddNamespace(ns, [provider])

[<assembly:TypeProviderAssembly()>]
do ()

在另一个项目中,我不想直接使用提供的类型,而是通过从其继承来使用:

type Provided = MyNamespace.MyProvider<"Host123">

type Derived() = 
    inherit Provided() //Cannot inherit a sealed type

但是我收到一条错误消息,指出所提供的类型是密封类,因此不能从其继承。

这是设计使然还是我缺少什么?

1 个答案:

答案 0 :(得分:4)

这是F#类型提供程序SDK中的默认行为。您可以在ProvidedTypeDefinition类的ProvidedTypes.fs中看到用于提供的类型定义的属性(在1241-1252行附近):

    static let defaultAttributes isErased = 
        TypeAttributes.Public ||| 
        TypeAttributes.Class ||| 
        TypeAttributes.Sealed ||| 
        enum (if isErased then int32 TypeProviderTypeAttributes.IsErased else 0)

您可以通过为构造函数的第五个参数显式传递TypeAttributes来覆盖它(您必须使用具有所有参数的构造函数)。在您的... omitted部分之后拾取,看起来像这样:

let derivableClassAttributes = TypeAttributes.Public ||| TypeAttributes.Class

let provided = 
    ProvidedTypeDefinition(false, 
                           TypeContainer.Namespace (K asm,ns), 
                           typeName, 
                           K (Some typeof<MyRuntimeType>), 
                           derivableClassAttributes, 
                           K None, 
                           [], 
                           None, 
                           None, 
                           K [||], 
                           true, 
                           true)