Generated type provider: extended sample

Can I create a generated type provider that provides types that are equivalent to the following F # code?

[<ProvidedTypeFlag("myTypeA")>]
type A(x:int) =
    inherit ValueType(x)
    member __.X = x+1

[<ProvidedTypeFlag("myTypeB")>]
type B(value:ValueType) =
    member __.Raw = value
    member __.toA = A(value.X)

    interface IComparable with
        member this.CompareTo obj =
            match obj with
            | :? B as other -> this.Raw.X.CompareTo (other.Raw.X)
            | _             -> invalidArg "obj" "not a B"

[<ProvidedTypeFlag("myTypeC")>]
type C() =
    static member Process(a:A) =
        seq {
            for x in [1..a.X] do
                yield B(ValueType(x))
        } |> Set.ofSeq

      

suppose i have the following types in the same assembly

// Value type that hold some data from 3rd party system
type ValueType (x:int) =
    member __.X = x

// Custom attribute that I want to have on provided types
[<AttributeUsage(AttributeTargets.Class, AllowMultiple=false)>]
type ProvidedTypeFlagAttribute(originName:string) =
   inherit System.Attribute()
   member __.OriginName = originName

      

If possible please provide an example on how to do this using ProvidedTypes.fs

+3


source to share


1 answer


Looking at this, it looks like you need a few small pieces.

Add custom attribute

I am using a little helper like this:

type CustomAttributeDataExt =
    static member Make(ctorInfo, ?args, ?namedArgs) = 
        { new CustomAttributeData() with 
            member __.Constructor =  ctorInfo
            member __.ConstructorArguments = defaultArg args [||] :> IList<_>
            member __.NamedArguments = defaultArg namedArgs [||] :> IList<_> }

      

The optional arguments args and namedArgs look a little simpler and also a code cleaner. When I want to add a custom attribute, if there are more than one, I usually add a few more typed helpers to make the code clearer in code:

module Attributes =
let MakeActionAttributeData(argument:string) =
    CustomAttributeDataExt.Make(typeof<ActionAttribute>.GetConstructor(typeof<string>),
                                [| CustomAttributeTypedArgument(typeof<ActionAttribute>, argument) |])

open Attributes
myProperty.AddCustomAttribute <| Attributes.MakeActionAttributeData("attributeData")

      



Adding a Constructor Calling the Base Type

Again, I have some helpers to think about:

    type Type with
    member x.GetConstructor(typ) =
        x.GetConstructor([|typ|])

    member x.TryGetConstructor(typ:Type) =
        x.GetConstructor(typ) |> function null -> None | v -> Some v

    ...

      

create your provided type as usual (remember to set IsErased = false) then

    //string ctor
    match providedType.TryGetConstructor(typeof<string>) with
    | None -> failwithf "No string constructor found for type: %s" providedType.Name
    | Some ctor -> let stringCtor = ProvidedConstructor([ProvidedParameter("theString", typeof<string>)], InvokeCode=Expr.emptyInvoke, BaseConstructorCall = fun args -> ctor, args)
                   providedController.AddMember(stringCtor)

      

I think other parts should be documented elsewhere, especially with the addition of regular members, etc.

+2


source







All Articles