c# - get end values from lambda expressions method parameters -


basically want values of parameters of called method this:

var x = 1; var = 2; var b = 3; do<homecontroller>(o => o.save(x, "jimmy", a+b+5, math.sqrt(81)));  public static void do<t>(expression<action<t>> expression) t : controller {   // values 1,jimmy,10,9 here } 

well, you'd need drill expression, find methodcallexpression, , @ arguments it. note don't have value of o, we've got assume arguments method don't rely on that. we're still assuming lambda expression relies on being methodcallexpression?

edit: okay, here's edited version evaluates arguments. however, assumes you're not using lambda expression parameter within arguments (which new object[1] - it's providing null parameter, effectively).

using system; using system.linq.expressions;  class foo {     public void save(int x, string y, int z, double d)     {     } }  class program {     static void main()     {         var x = 1;         var = 2;         var b = 3;         showvalues<foo>(o => o.save(x, "jimmy", + b + 5, math.sqrt(81)));     }      static void showvalues<t>(expression<action<t>> expression)     {         var call = expression.body methodcallexpression;         if (call == null)         {             throw new argumentexception("not method call");         }         foreach (expression argument in call.arguments)         {             lambdaexpression lambda = expression.lambda(argument,                                                          expression.parameters);             delegate d = lambda.compile();             object value = d.dynamicinvoke(new object[1]);             console.writeline("got value: {0}", value);         }     } } 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -