How To? Use an Expression Tree to call a Generic Method when the Type is only known at runtime.

Posted by David Williams on Stack Overflow See other posts from Stack Overflow or by David Williams
Published on 2010-03-12T14:41:35Z Indexed on 2010/03/12 14:47 UTC
Read the original article Hit count: 249

Filed under:
|
|

Please bear with me; I am very new to expression trees and lambda expressions, but trying to learn. This is something that I solved using reflection, but would like to see how to do it using expression trees.

I have a generic function:

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 );

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about lambda