gpt4 book ai didi

reflection - 我将如何发出受歧视的联盟?

转载 作者:行者123 更新时间:2023-12-03 18:30:51 25 4
gpt4 key购买 nike

如果我有这样的东西:

type public ServiceActionType =
| ServiceRequest
| ServiceResponse
| ServiceEventInvocation
| ServiceEventSubscriptionRequest
| ServiceMetadataRequest

然后我如何将这些项目之一添加到堆栈中,如:

IlGen.Emit(OpCodes.???, ???)

最佳答案

现有答案很好,但请注意 Microsoft.FSharp.Reflection namespace 有一些帮助器,可以轻松获取您关心的成员:

open Reflection
let cases = FSharpType.GetUnionCases(typeof<ServiceActionType>)
let serviceRequestMethod = FSharpValue.PreComputeUnionConstructorInfo(cases.[0])

结果与typeof<ServiceActionType>.GetMethod("get_ServiceRequest")相同但您不必担心知道编译后的形式,它会根据联合案例是否有字段而有所不同。

关于reflection - 我将如何发出受歧视的联盟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15573039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com