How do I create dynamic type providers based on user input?

51 Views Asked by At

I want to allow users to provide complex objects, i.e System.Diagnostics.Process[].

I see the documentation of type providers for JSON, CSV, HTML, etc. but I didn't see any type providers for normal Microsoft classes and the documentation to create a type provider appears to be manual.

For example, to create a CSV file Tomas Petricek gave the below code in How to write a CSV in F#

type MyCsvType = CsvProvider<Schema = "A (int), B (int), C (float), D (int), E (int), F (int)", HasHeaders = false>

let myCsvBuildRow (x:Family) = 
  MyCsvType.Row(x.Month,x.Year,x.Income,x.Family,x.Dogs,x.Cats)
let myCsvBuildTable data = 
  new MyCsvType(Seq.map myCsvBuildRow data)

let myCsv = family |> myCsvBuildTable
myCsv.SaveToString()

I could get all process from my machine and manually set the headers, etc. But if I want to get the services, I would need to do it again. As Microsoft classes are known, is there an option to create a type provider based on the objects properties dynamically?

Ideally the code should look like this:

let processes = System.Diagnostics.Process.GetProcesses()

type MSCsvType = CsvProvider<Schema = System.Diagnostics.Process, HasHeaders = true>
MSCsvType(processes)
MSCsvType.Save("Path\To\Save\processes.csv")
0

There are 0 best solutions below

Related Questions in F#