简体   繁体   English

如何在F#中获取给定联合类型的每个联合案例的类型

[英]How to get the type of each union case for a given union type in F#

I am wondering in the F# code below how to fetch the type associated with each union case via reflection 我想知道下面的F#代码中如何通过反射获取与每个联合案例关联的类型

type AccountCreatedArgs = {
    Owner: string
    AccountId: Guid
    CreatedAt: DateTimeOffset
    StartingBalance: decimal
}

type Transaction = {
    To: Guid
    From: Guid
    Description: string
    Time: DateTimeOffset
    Amount: decimal
}

type AccountEvents =
    | AccountCreated of AccountCreatedArgs
    | AccountCredited of Transaction
    | AccountDebited of Transaction

I tried using FSharpType.GetUnionCases(typeof<AccountEvents>) but UnionCaseInfo does not provide any information about the case type (only the declaring type aka AccountEvents so not really useful in my case) =/ 我尝试使用FSharpType.GetUnionCases(typeof<AccountEvents>)UnionCaseInfo不提供有关案例类型的任何信息(仅声明类型,也称为AccountEvents因此在我的案例中并不真正有用)= /


The answer of glennsl really helped me https://stackoverflow.com/a/56351231/4636721 glennsl的回答确实帮助了我https://stackoverflow.com/a/56351231/4636721

What I really found handy in my case was: 我真正感到方便的是:

let getUnionCasesTypes<'T> =
    Reflection.FSharpType.GetUnionCases(typeof<'T>)
    |> Seq.map (fun x -> x.GetFields().[0].DeclaringType)

UnionCaseInfo has a GetFields method which returns an array of PropertyInfo s which describe each field/argument of the union case. UnionCaseInfo有一个GetFields方法,该方法返回一个PropertyInfo数组,该数组描述联合案例的每个字段/参数。 For example: 例如:

FSharpType.GetUnionCases(typeof<AccountEvents>)
    |> Array.map(fun c -> (c.Name, c.GetFields()))
    |> printfn "%A"

will print 将打印

[|("AccountCreated", [|AccountCreatedArgs Item|]);
  ("AccountCredited", [|Transaction Item|]);
  ("AccountDebited", [|Transaction Item|])|]

The name assigned to a single field union case is "Item", and if multiple is "Item1", "Item2" etc. The field type itself can be retrieved from the PropertyType property of PropertyInfo , so: 分配给单个字段联合情况下,名称为“项目”,并且如果有多个是“Item1的”,“项目2”等字段类型本身可以从检索PropertyType的属性PropertyInfo ,所以:

FSharpType.GetUnionCases(typeof<AccountEvents>)
    |> Array.map(fun c -> (c.Name, c.GetFields() |> Array.map(fun p -> p.PropertyType.Name)))
    |> printfn "%A"

will thus print 因此将打印

[|("AccountCreated", [|"AccountCreatedArgs"|]);
  ("AccountCredited", [|"Transaction"|]);
  ("AccountDebited", [|"Transaction"|])|]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM