dynamicexpresso / DynamicExpresso

C# expressions interpreter
http://dynamic-expresso.azurewebsites.net/
MIT License
2.01k stars 379 forks source link

implicit `this` reference #232. #233

Closed ealeykin closed 2 years ago

ealeykin commented 2 years ago

Allows to implicitly access this identifier set either as a Variable or passed as a Parameter to have more concise expression.

In other word an emulation of this implicit argument passed to a method call.

const string Name = "John";
var context = new KeyValuePair<string, string>(nameof(Name), Name);
var interpreter = new Interpreter().SetVariable("this", context);

Assert.AreEqual(Name, interpreter.Eval("this.Value"));
Assert.AreEqual(Name, interpreter.Eval("Value"));

or

const string Name = "John";
var context = new KeyValuePair<string, string>(nameof(Name), Name);
var parameter = new Parameter("this", context.GetType());
var interpreter = new Interpreter();

Assert.AreEqual(Name, interpreter.Parse("this.Value", parameter).Invoke(context));
Assert.AreEqual(Name, interpreter.Parse("Value", parameter).Invoke(context));

https://github.com/dynamicexpresso/DynamicExpresso/issues/232

ealeykin commented 2 years ago

I would say it's not a special keyword, but rather a special identifier. Please check my latest readme notes commit.