Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Expressions and Dynamic String Manipulation

Tags:

c#

lambda

linq

So, I am trying to learn how to put together my own expressions, pass objects and compile to retrieve generated result, and I am stuck trying to understand exactly where my object instance goes in all of this.

So this is what I have gotten so far from reading through the code and stepping through

  1. Create your object instance, your expressionstring, and parameters.

    T SampleString = "Some String I have";
    var operation= "it.Replace(@0, @1)";
    var operationParameters = new [] { "e", "CLOWN"};
    
  2. Create a ParameterExpression object to specify the type of parameter your operation will be performed on

    ParameterExpression[] parameters = new ParameterExpression[] { Expression.Parameter(typeof(T), "") };
    
  3. Using the ExpressionParser class, create the expression you need to be executed against your object there

    ExpressionParser parser = new ExpressionParser(parameters, operation, operationParameters );
    
  4. Called the ExpressionParser Parse method to retrieve the generated Expression, passing it the type of the result you want

    var generatedExpression = parser.Parse(typeof(String));
    
  5. Now call the Expression.Lamba, passing it the generatedExpression, and the item

    var StringReplaceresult = Expression.Lambda<Func<T,String> >(generatedExpression , parameters).Compile()(item);
    

I am not quite sure if the above is correct, or where exactly the issue I am having with it starts. I do know that my Compile fails (5). The message is about not passing in the right number of parameters to the Expression.Lamba method there. But I wonder if that is really where the problem is as, again, I am not sure I get this even 60%, so I would appreciate it is someone would please correct my work above, where necessary.

like image 261
Kobojunkie Avatar asked Mar 18 '26 13:03

Kobojunkie


1 Answers

I'm assuming you're using the Dynamic Linq Query Library described by Scott Guthrie:

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

If so, I think you want:

string sampleString = "some string I have";
var operation= "it.Replace(@0, @1)";
var operationParameters = new [] { "e", "CLOWN"};
Expression<Func<string, string>> expr = DynamicExpression.ParseLambda<string, string>(operation, operationParameters);
string result = expr.Compile().Invoke(sampleString);

When I run this in LinqPad the value of result is "somCLOWN string I havCLOWN"

The DynamicExpression.ParseLambda allows you to specify the parameter types as generic type arguments, rather than doing it by explicit creation of a ParameterExpression array as you were doing.

The ParseLambda<> call returns a strongly typed Expression<TDelegate> object and it's Compile() method compiles the underlying lambda expression into executable code and returns it as a delegate of the correct type that can then be invoked. That means the Invoke() returns an object of the correct type(string in this case) rather than an object that has to be cast. So even though you start off with non-strongly-typed code, you get back to type-safety as quickly as possible.

http://msdn.microsoft.com/en-us/library/bb345362.aspx

p.s.

And in the source code I have, ExpressionParser is internal, you naughty boy/girl ;o)

like image 177
Mike Goodwin Avatar answered Mar 20 '26 03:03

Mike Goodwin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!