I had a piece of code that takes lambda expressions at runtime that I can compile and invoke.
Something thing; Expression<Action<Something>> expression = (c => c.DoWork()); Delegate del = expression.Compile(); del.DynamicInvoke(thing);
To save runtime, I saved these compiled delegates to cache, Dictionary<String, Delegate> , which is the string of the lambda expression.
cache.Add("(Something)c => c.DoWork()", del);
For the same calls, it worked fine. However, I realized that I could get equivalent lambdas, such as "d => d.DoWork ()", on which I should use the same delegate, but I was not.
This made me wonder if there was a clean path (read “not using String.Replace”, I already did it as a temporary fix) to replace the elements in the lambda expression, for example, possibly replacing them with arg0 so that both
(c => c.DoWork()) and (d => d.DoWork())
are converted and compared as (arg0 => arg0.DoWork()) , using something fuctionnally similar to an injection of an expression. Parameter (Type, Name) in lambda.
Is it possible? (Answers may include C # 4.0)
c # lambda expression delegates
Dynami le savard
source share