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

标签 reflection f#

如果我有这样的东西:

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/

相关文章:

java - 通过外部化进行逆向计算

reflection - 在 F# 中,如何获取函数的参数?

Java - 从反射中排除父类(super class)字段

lambda - F# 查询内部集合

F#:将旧的 IEnumerable.GetEnumerator() 样式迭代器转换为 seq

functional-programming - 在不使用嵌套 map 的情况下为列表列表实现 map 功能

scala - 访问 Scala 中的注释值

.net - 获取不带 "Attribute"的属性类型名称

functional-programming - 如何在不断变化的 Rest/JSON 端点上使用 F# 类型提供程序?

f# - 记录等于或 GetHashCode 抛出 NullReferenceException