Skip to content

Extensions methods/properties support for Spring Expressions #221

@amadare42

Description

@amadare42

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions