生成类型提供程序:高级示例

3

是否可能编写一个生成的类型提供程序,提供等同于以下F#代码的类型?

[<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

假设我有以下类型在同一个程序集中:
// 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

如果可能的话,请提供一个使用ProvidedTypes.fs进行操作的示例。

是的,这是可能的,我有类似的实现接口和自动释放等功能。 - 7sharp9
1个回答

2

看起来你需要几个小块。

添加自定义属性

我使用这样的辅助程序:

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

可选参数和命名参数使得代码更简洁易读。当我想要添加自定义属性时,如果有多个,通常会添加更多的类型辅助函数,以在代码中使事物更清晰明了:

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

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

添加调用基类型的构造函数

同样,我有一些反射小工具:

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

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

    ...

按照惯例创建您提供的类型(记得将IsErased设置为false),然后

    //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)

我认为其他部分应该在其他地方记录,特别是添加普通成员等。


Profile259CustomAttributeData()的替代方法是什么?(因为那里没有可用的构造函数... - Sergey Tihon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接