问题
I am creating a simple test type provider.
I want to provide a string, and return a type with the type name equal to that provided string.
But the result doesn't work, saying that BasicProvider is a non-generic type.
Error:
The non-generic type 'SimpleStringProvider.BasicProvider' does not expect any type arguments, but here is given 1 type argument(s)
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 ()
Here is how I test it in a script:
#r @".\src\bin\Debug\netstandard2.0\SimpleStringProvider.dll"
open SimpleStringProvider
type X = SimpleStringProvider.BasicProvider<"test">
来源:https://stackoverflow.com/questions/50374929/the-non-generic-type-type-does-not-expect-type-arguments