spring-net icon indicating copy to clipboard operation
spring-net copied to clipboard

Extensions methods/properties support for Spring Expressions

Open amadare42 opened this issue 2 years ago • 1 comments

Hey. I would like to have functionality to add extension methods to objects. So user could define spring (or .net) lambda function for specific type. For example:

var vars = new Dictionary<string, object>{ 
 { "Foo", new Foo() { InternalValue = 2 } }
};
Expressions.RegisterExtensionFunction<Foo>(vars, "SpelExtension", "{|self, arg| $self.InternalValue + $arg}");
Expression.Parse("Foo.SpelExtension(1)").GetValue(null, vars); // result in Foo.InternalValue + 1 -> 3

Expressions.RegisterExtensionFunction<Foo, int>(vars, "CsExtension", (self, arg) => sef.InternalValue + arg);
Expression.Parse("Foo.CsExtension(1)").GetValue(null, vars); // result in Foo.InternalValue + 1 -> 3

Expressions.RegisterExtensionProperty<Foo>(vars, "SpelProp", "{|self| $self.InternalValue + 2}");
Expression.Parse("Foo.SpelProp").GetValue(null, vars); // result in Foo.InternalValue + 2 -> 4

Expressions.RegisterExtensionProperty<Foo>(vars, "CsProp", (self, arg) => sef.InternalValue + 2);
Expression.Parse("Foo.CsProp").GetValue(null, vars); // result in Foo.InternalValue + 2 -> 4

I have a quick and dirty POC that provides this functionality. I can clean it up and create PR. Are maintainers interested in this functionality?

amadare42 avatar May 26 '22 15:05 amadare42

Sorry for the delay. Yes, all contributions are always appreciated!

lahma avatar Jul 17 '22 07:07 lahma