问题描述
如何在F#中获得以下CIL构造函数(如System.Relection.ConstructorInfo
)?
[mscorlib]System.Func`3<int32,int32,int32>::.ctor(object,native int)
我尝试了以下操作,但没有得到结果:
typeof<int * int * int>.GetConstructor([|typeof<obj> ; typeof<nativeint>|]);;
val it : System.Reflection.ConstructorInfo = <null>
还可以动态地进行吗?假设不是3个int类型,而是随机的System.Type list
。怎么样?
解决方法
发现:
System.Linq.Expressions.Expression.GetDelegateType([|typeof<int> ; typeof<int> ; typeof<int>|]).GetConstructor([|typeof<obj> ; typeof<nativeint>|]).DeclaringType;;
GetDelegateType
方法采用类型数组,因此可以动态使用。