如果我有这样的东西:
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/