如何使用表达式目录树调用时,类型在运行时只知道一个泛型方法? [英] How can I use an expression tree to call a generic method when the Type is only known at runtime?
问题描述
这是什么,我解决了使用反射,但想看看如何使用表达式树做
This is something that I solved using reflection, but would like to see how to do it using expression trees.
我有一个泛型函数:
private void DoSomeThing<T>( param object[] args ) {
// Some work is done here.
}
这是我需要从其他地方在我的课打电话。现在,通常情况下,这将是很简单:
that I need to call from else where in my class. Now, normally, this would be be simple:
DoSomeThing<int>( blah );
但只有我知道,在那个我与工作设计时间 INT
。当我不知道是什么类型,直到运行时是我需要的帮助。就像我说的,我知道如何通过反射做,但我想通过表达式目录树这样做,因为我的(非常有限)的理解是,我可以这样做。
but only if I know, at design time that I am working with an int
. When I do not know the type until runtime is where I need the help. Like I said, I know how to do it via reflection, but I would like to do it via expression trees, as my (very limited) understanding is that I can do so.
任何建议或指向的网站在那里我能得到这样的认识,最好有示例代码?
Any suggestions or points to sites where I can get this understanding, preferably with sample code?
推荐答案
<击>然后,只需创建一个委托,并调用它击>(不表达,当然,p)
Then just create a delegate and call it. (not in an expression, of course ;p)
更新:
一般情况下,我更喜欢使用泛型类型对于这一点, Activator.CreateInstance
只需要较少的工作。一切都取决于虽然你的情况。
Generally, I prefer to use generic types for this, Activator.CreateInstance
just requires less work. All depends on your situation though.
这篇关于如何使用表达式目录树调用时,类型在运行时只知道一个泛型方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!