How can I use an expression tree to call a generic method when the Type is only known at runtime?

前端 未结 2 2132
甜味超标
甜味超标 2021-02-13 11:36

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 DoSo         


        
2条回答
  •  抹茶落季
    2021-02-13 11:50

    MethodInfo.MakeGenericMethod

    Then just create a delegate and call it. (not in an expression, of course ;p)

    Update:

    Generally, I prefer to use generic types for this, Activator.CreateInstance just requires less work. All depends on your situation though.

提交回复
热议问题