Reputation: 49945
I'm trying to build my first toy-like Type Provider. What I'm trying to achieve is to have dynamically generated properties of dynamically generated types.
collection
|> getItems
|> Seq.map(fun mapItem ->
let nestedType = ProvidedTypeDefinition(assembly, ns, "MyNestedType", None)
let ctor = ProvidedConstructor(List.Empty)
nestedType.AddMember ctor
mapItem.Value
|> Seq.map(fun pair ->
ProvidedProperty(fst(pair), typeof<string>,
GetterCode = fun [_] -> <@@ snd(pair) @@>))
|> Seq.toList
|> nestedType.AddMembers
ProvidedProperty(mapItem.Key, nestedType,
GetterCode = fun [map] ->
// ?? Runtime Exception
let inst = nestedType.GetConstructors().[0].Invoke([||])
<@@ inst @@>
))
|> Seq.toList
|> ty.AddMembers
ty
How should I instantiate dynamically generated type ?
Upvotes: 3
Views: 200
Reputation: 80724
What you're trying to do here is instantiate your type while building the provider, and then include that new instance in the body of the property. It should be abundantly clear that you can't instantiate the provided type before you've finished providing it.
What you really want to do is take your provided constructor and build a quotation that calls it. You can't have the compiler build the quotation for you, because in order for the compiler to compile the body of the quotation, it needs to "see" all types/methods/functions inside, and your type is not yet ready. But you can create the quotation manually by using the various constructors under Quotations.Expr
. In this case, NewObject
is suitable:
GetterCode = fun [map] -> Expr.NewObject (ctor, [])
Upvotes: 0
Reputation: 243041
I'm assuming this is an erasing type provider (those are the easy ones, so they're better choice for getting started). If that's not the case, then disregard my answer.
In the GetterCode
, you do not need to create instance of the nested provided type. You just need to create an instance of the type that it is erased to.
In your case, nestedType
is erased to None
and so the constructor just needs to create a System.Object
value, so you should be able to use:
ProvidedProperty(mapItem.Key, nestedType,
GetterCode = fun [self] -> <@@ obj() @@>)
In reality, you'll probably want to erase to some type that lets you keep some data that the nested type is supposed to access. If the nested type was erased to, say, MyRuntimeType
, you could then write:
let parameter = mapItem.WhateverYouWantHere
ProvidedProperty(mapItem.Key, nestedType,
GetterCode = fun [self] -> <@@ MyRuntimeType(parameter) @@>)
Note that I'm using let
to capture the value of the primitive parameter
type, so that the compiler can serialize the quotation (you cannot capture complex object types in a quotation).
Upvotes: 3