Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7791667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:57:31+00:00 2026-06-01T21:57:31+00:00

I would like to craft a simple record type based on fields provided. That

  • 0

I would like to craft a simple record type based on fields provided.

That is :

let rectype = MakeRecordType(['fieldname1'; 'fieldname2'])

Going directly to type providers looks like heavy gunpower for such a simple task.

Are there any other way ?

update

I found the following question which look very similar
Creating F# record through reflection

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:57:33+00:00Added an answer on June 1, 2026 at 9:57 pm

    Putting aside the usefulness of the end result, a snippet below achieves exactly what you asked for in the spirit of my other related answer:

    #if INTERACTIVE
    #r @"C:\Program Files (x86)\Microsoft F#\v4.0\FSharp.Compiler.dll"
    #r @"C:\Program Files (x86)\FSharpPowerPack-1.9.9.9\bin\FSharp.Compiler.CodeDom.dll"
    #endif
    
    open System 
    open System.CodeDom.Compiler 
    open Microsoft.FSharp.Compiler.CodeDom
    open Microsoft.FSharp.Reflection
    
    type RecordTypeMaker (typeName: string, records: (string*string) []) = 
        let _typeDllName = "Synth"+typeName+".dll"
        let _code = 
            let fsCode = new System.Text.StringBuilder()
            fsCode.Append("module ").Append(typeName).Append(".Code\ntype ").Append(typeName).Append(" = {") |> ignore
            for rec' in records do fsCode.Append("  ").Append(fst rec').Append(" : ").Append(snd rec').Append(";\n") |> ignore
            fsCode.Append("}").ToString()
        let _compiled =
            use provider = new FSharpCodeProvider() 
            let options = CompilerParameters([||], _typeDllName) 
            let result = provider.CompileAssemblyFromSource( options, [|_code|] ) 
            result.Errors.Count = 0
        let mutable _type: Type = null
    
        member __.RecordType
            with get() = if _compiled && _type = null then
                             _type <- Reflection.Assembly.LoadFrom(_typeDllName).GetType(typeName+".Code+"+typeName)
                         _type
    

    A sketch implementation of RecordTypeMaker accepts an arbitrary Record type definition containing type name and array of field names accompanied by field type names. Then behind the curtain it assembles a piece of F# code defining the requested Record type, compiles this code via CodeDom provider, loads container assembly and provides access to this newly created synthetic Record type via Reflection. A test snippet

    let myType = RecordTypeMaker("Test", [|("Field1", "string"); ("Field2", "int")|]).RecordType
    printfn "IsRecordType=%b" (FSharpType.IsRecord(myType))
    printfn "Record fields: %A" (FSharpType.GetRecordFields(myType))
    

    demonstrates for a purely synthetic type myType the proof of concept:

    IsRecordType=true
    Record fields: [|System.String Field1; Int32 Field2|]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to craft a case-insensitive regex (for JavaScript) that matches street names,
Would like to make anapplication in Java that will not automatically parse parameters used
Would like to know the c# code to actually retrieve the IP type: Static
I would like to craft a GWT CellTree with an optional pop-up menu triggered
I've got some PHP cruft that I would like to delegate methods. Sort of
would like to allow visitors to a web page to upload photos that will
Would like to create a method that instantiate objects. - (NSArray *) make3Of :
Would like to be able to set colors of headings and such, different font
Would like to know what a programmer should know to become a good at
Would like to know how to integarate cruise control with maven? Cruise Control comes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.