如何找出表达式指定的类型?
例如,假设我有一个方法,它将以下内容作为参数:
Expression<Func<T, object>> path
如何确定表达式中指定的“对象”的类型?更具体地说,我想确定它是否是集合类型(例如IEnumerable
)
For example, let's say I have a method that takes the following as a parameter:
Expression<Func<T, object>> path
How do I determine the type of the 'object' specified in the expression? More specifically, I'd like to determine if it's a collection type (eg. IEnumerable
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您已经在使用泛型,为什么不继续使用呢?
You're already using generics, so why not go all the way?
或者,您可以执行类似以下操作(这更符合您的泛型类型参数):
Alternatively, you could do something like the following (which is more inline with you generic type arguments):
Expression.Type
将回答这个问题(表达式的“结果”类型)。为了更深入、更普遍地挖掘,您需要根据
Expression
实例的实际、运行时和类型来考虑不同的可能性 - 其中许多Expression
的子类型您实际上有。Expression.Type
will answer this (the "result" type of the expression).To dig deeper and more generally you will need to consider different possibilities depending on the actual, runtime, type of the
Expression
instance—which of the many subtypes ofExpression
do you actually have.