我正在创建一个简单的测试类型提供程序。
我想提供一个字符串,并返回一个类型名称等于提供字符串的类型。
但结果不起作用,说BasicProvider是非泛型类型。
错误:
非泛型类型'SimpleStringProvider.BasicProvider'没有 期望任何类型的参数,但这里给出了1个类型的参数
module SimpleStringProvider
open ProviderImplementation
open ProviderImplementation.ProvidedTypes
open Microsoft.FSharp.Core.CompilerServices
open System.Reflection
[<TypeProvider>]
type BasicProvider (config : TypeProviderConfig) as this =
inherit TypeProviderForNamespaces (config)
let ns = "SimpleStringProvider.StringProvider"
let asm = Assembly.GetExecutingAssembly()
let staticParams = [ProvidedStaticParameter("value", typeof<string>)]
let createTypes () =
let myType = ProvidedTypeDefinition(asm, ns, "StringProvider", Some typeof<obj>)
// let myProp = ProvidedProperty("MyProperty", typeof<string>, isStatic = true, getterCode = (fun args -> <@@ "Hello world" @@>))
// myType.AddMember(myProp)
let instantiationFunction _ paramValues =
let typeName = (paramValues |> Array.head).ToString()
ProvidedTypeDefinition(
asm,
ns,
typeName,
Some typeof<obj>
)
do myType.DefineStaticParameters(
parameters = staticParams,
instantiationFunction = instantiationFunction
)
do this.AddNamespace(ns, [myType])
[<assembly:TypeProviderAssembly>]
do ()
以下是我在脚本中测试它的方法:
#r @".\src\bin\Debug\netstandard2.0\SimpleStringProvider.dll"
open SimpleStringProvider
type X = SimpleStringProvider.BasicProvider<"test">