是否可能编写一个生成的类型提供程序,提供等同于以下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进行操作的示例。