我怎样才能模仿“typeof (T)”呢?使用System.Linq.Expressions API
本文关键字:使用 System API Expressions Linq typeof | 更新日期: 2023-09-27 18:01:30
我试图创建一个方法,将使用System.Linq.Expressions
API从对象列表中创建DataTable
,但我无法弄清楚如何生成以下IL,当我反编译表达式typeof (int)
时。
IL_0000: nop
IL_0001: ldtoken System.Int32
IL_0006: call System.Type.GetTypeFromHandle
IL_000B: call LINQPad.Extensions.Dump<Object>
IL_0010: pop
IL_0011: ret
目前,我正试图通过调用Type.GetType("System.Int")
来避开这个问题,但如果可能的话,我想生成typeof (int)
的代码。
仅使用Expression.Constant
并传入typeof(int)
作为值:
var expression = Expression.Constant(typeof(int), typeof(Type));
无论如何,这就是在lambda表达式中使用typeof
时发生的情况:
Expression<Func<Type>> func = () => typeof(T);