当前位置 : 主页 > 手机开发 > 其它 >

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

来源:互联网 收集:自由互联 发布时间:2021-06-19
我有这个基本的生成F#类型提供者 [TypeProvider]type MyTypeProvider(config : TypeProviderConfig) as this = inherit TypeProviderForNamespaces(config) let ns = "MyNamespace" let asm = Assembly.LoadFrom(config.RuntimeAssembly) let b
我有这个基本的生成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

但是我收到一个错误,提到所提供的类型是一个密封类,因此无法继承.

这是设计还是我错过了什么?

这是F#Type Provider SDK中的默认行为.您可以在ProvideTypeDefinition类(第1241-1252行)中看到 ProvidedTypes.fs中用于提供类型定义的属性:

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

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

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)
网友评论