我有这个基本的生成F#类型提供者 [TypeProvider]type MyTypeProvider(config : TypeProviderConfig) as this = inherit TypeProviderForNamespaces(config) let ns = "MyNamespace" let asm = Assembly.LoadFrom(config.RuntimeAssembly) let b
[<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)