您可以提供嵌套类型,这些类型可以基于静态参数。在您的示例中
Provided
是一种类型,并且
Provided.One
可以是嵌套类型。
要做到这一点,您可以这样写:
[<TypeProvider>]
type public SingleStringTypeProvider(cfg:TypeProviderConfig) as this =
inherit TypeProviderForNamespaces()
// Generate namespace and the main type provider
let asm = System.Reflection.Assembly.GetExecutingAssembly()
let ns = "Samples"
let stringProvider = ProvidedTypeDefinition(asm, ns, "SingleStringTypeProvider", Some(typeof<obj>))
// Define one static parameter with type name
let parameter = ProvidedStaticParameter("TypeName", typeof<string>)
do stringProvider.DefineStaticParameters([parameter], fun typeName args ->
// Create the main type (this corresponds to `Provided`)
let resTy = ProvidedTypeDefinition(asm, ns, typeName, Some typeof<IniFile>)
// Add a nested type as a member using the name from the parameter
let typeName = args.[0] :?> string
ProvidedTypeDefinition(typeName, None)
|> resTy.AddMember
resTy )
[<assembly:TypeProviderAssembly>]
do()
我还没有测试过这个,所以您可能需要做一些调整,但我认为它应该可以工作。