I don't get how to provide strongly typed types through type provider. All the exemples I see have the same type as a result, independently of the input.
MiniCsvTypeProvider provides only double. RegexTypeProvider provides only matches.
Is it possible to provide properties of different types depending on a parameter supplied to the type provider ?
if somevariable then
proptype = typeof<int>
else
proptype = typeof<string>
let staticProp = ProvidedProperty(propertyName = "property",
propertyType = propType,
GetterCode= (fun [arg] -> <@@ %%arg :?> propType @@>))
Yes, it's easy to provide different types depending on the input. As a very simple example, you could do something like:
let propType, propValue =
if somevariable then
typeof<int>, <@@ 1 @@>
else
typeof<string>, <@@ "test" @@>
let prop = ProvidedProperty("property", propType, GetterCode = fun [_] -> propValue)
To expand this along the lines you're suggesting, you could define the entire getter in the conditional:
let propType, propGetter =
if somevariable then
typeof<int>, fun [arg] -> <@@ %%arg : int @@>
else
typeof<string>, fun [arg] -> <@@ %%arg : string @@>
let prop = ProvidedProperty("property", propType, GetterCode = fun [_] -> propValue)
However, note that you then need to ensure that the representation that you call the property on is an int
or string
respectively. Also note that in contrast to your chosen naming (staticProp
), these are not static properties, since you're passing a receiver (arg
) to the getter and haven't marked the ProvidedProperty
static.