F# generating types in a type extension function w

2019-07-13 13:08发布

问题:

I have the following problem:

Within my type provider, I need to extend a generic type, that I have previously defined, with a method that will return an instance of this generic type. What I mean is, let's say we have :

type receiveType<'a> = class
   val Next:int
   val Type:string
   new (state,stateType) =
      {Next = state;Type = stateType;}                    
   end

later within my typeprovider I want to extend the type with the following function:

[<TypeProvider>] 
type ProviderTest() as this= (
   inherit TypeProviderForNamespaces() 
   let ns = "typeProviderTest.Provided"    
   let asm = Assembly.GetExecutingAssembly()

   let makeAType (s:string) =
      let typage =
         ProvidedTypeDefinition(asm, ns, s, Some typeof<obj>) 
      typage

   type receiveType<'a> with
      member this.receive(hey:'a,next:string)= 
         // generate a type from a string for instance
         // "hello" -> type helloType
         generateTypeFromString(next) 
        // let typage = ProvidedTypeDefinition(asm, ns, next, Some typeof<obj>)
        new receiveType<nextType>(5,"hello")   

The goal here for me is to generate an instance of the type receiveType<TypeHello> , iterate through a structure (Finite State Machine) to get the next possible message I can receive ( for instance "goodMorning"), create the type associated ( TypeGoodMorning ) and then generate an instance of receiveType<TypeGoodMorning>.

Thank you.