Search code examples
f#type-providers

In F# type providers how do I add a static class to a provided type?


How can I add a type to another ProvidedType, specifying that that type is a static class?


Solution

  • It's not immediately obvious how to do this but all you have to do is add the appropriate metadata attributes in the form of TypeAttributes:

    //create a providedtype
    let myStaticType = ProvidedTypeDefinition("Tags", Some typeof<obj>, isErased = false)
    
    //set the TypeAttributes on the type
    myStaticType.SetAttributes (TypeAttributes.Public ||| TypeAttributes.Class ||| TypeAttributes.Sealed ||| TypeAttributes.Abstract)
    
    //Add the static type to another type
    parentType.AddMember myStaticType
    

    A static type is simply a type that has the Abstract and Sealed TypeAttributes