OK, I've solved the issue. It turned out that we must create ProvidedAssembly inside DefineStaticParameters() callback method, not outside it.
Wrong:
[<TypeProvider>]
type public TypeProvider1() as this =
inherit TypeProviderForNamespaces()
// this is wrong place to create the assembly!
let assembly = ProvidedAssembly (System.IO.Path.ChangeExtension(System.IO.Path.GetTempFileName(), ".dll"))
// ...other standard stuff here...
let newT = ProvidedTypeDefinition(thisAssembly, rootNamespace, "TypeProvider1", Some baseTy)
let staticParams = [ProvidedStaticParameter("param1", typeof<string>)]
do newT.DefineStaticParameters(
parameters=staticParams,
instantiationFunction=(fun typeName paramValues ->
match paramValues with
| [| :? string as param1 |] ->
let ty = ProvidedTypeDefinition(
thisAssembly,
rootNamespace,
typeName,
Some baseTy,
HideObjectMethods=true)
// ...add method, properties and the like...
// finally, add the type into the assembly
do assembly.AddTypes [newT])
Right:
[<TypeProvider>]
type public TypeProvider1() as this =
inherit TypeProviderForNamespaces()
// ...standard stuff here as before...
let newT = ProvidedTypeDefinition(thisAssembly, rootNamespace, "TypeProvider1", Some baseTy)
let staticParams = [ProvidedStaticParameter("param1", typeof<string>)]
do newT.DefineStaticParameters(
parameters=staticParams,
instantiationFunction=(fun typeName paramValues ->
match paramValues with
| [| :? string as param1 |] ->
let ty = ProvidedTypeDefinition(
thisAssembly,
rootNamespace,
typeName,
Some baseTy,
HideObjectMethods=true)
// ...add method, properties and the like...
// we must create the assembly here, inside the callback
let assembly = ProvidedAssembly (System.IO.Path.ChangeExtension(System.IO.Path.GetTempFileName(), ".dll"))
// then add the type into it
do assembly.AddTypes [newT])
After this change several types of this TP can be declared in same project without any problem.