0

I want to be able to dynamically construct an Action from a string, so that I am able to operate on an output object when taking an input object.

This post essentially asks "Is there a library/framework that will allow me to construct an Action from string?".

I have tried using Flee to accomplish this, but it seems you are unable to set variables from within the expressions that Flee compiles. As such, it does not quite meet the needs of my project.

I would like to achieve this functionality:

var source = new Person();

// ... define source

var target = new Hat();

// ... define target

var expression = "hat.HatSize = person.HeadSize";
var action = ActionBuilder.WithParameters(source, target).CompileExpression(expression);

action(source, target);

Within ActionBuilder I would want to achieve similar functionality to Flee, in that the type names of exposed variables are exposed to the expression that is being compiled.

My apologies if this is not the correct format in which I should be asking this question, or if I have missed a framework/library that is glaringly obvious.

fishycrakers
  • 71
  • 3
  • 10
  • Possible duplicate of [Is it possible to dynamically compile and execute C# code fragments?](https://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-sharp-code-fragments) – Arthur Attout Sep 30 '19 at 12:06
  • 2
    @ArthurAttout That dupe is a little outdated. Meanwhile, there is [Roslyn](https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/), [C#Script](https://blogs.msdn.microsoft.com/cdndevs/2015/12/01/adding-c-scripting-to-your-development-arsenal-part-1/) ... – Fildor Sep 30 '19 at 12:14
  • 1
    Yup, and a [Roslyn solution is mentioned](https://stackoverflow.com/a/29417053/7540393) – Arthur Attout Sep 30 '19 at 12:15
  • Point granted, while it isn't the accepted answer. But by now OP should be aware of it ... – Fildor Sep 30 '19 at 12:17
  • The intention of this post was to ascertain whether a solution existed that would avoid me having to touch the compiler. For example, something similar to the Expression library. I will look at a Roslyn solution if the consensus is that there is no other approach. – fishycrakers Sep 30 '19 at 12:17
  • 1
    C#Script as mentioned by Fildor seems a very easy and convenient solution, that doesn't require to touch anything regarding the compiler – Arthur Attout Sep 30 '19 at 12:18
  • I have been using it some time ago and back then it was quite convenient. By now it should be even better, I would guess. You can even quite conveniently debug scripts and once you understand the concept, it is fairly easy. – Fildor Sep 30 '19 at 12:20
  • @Fildor If you post a response I'll mark it as accepted.C#Script seems to be sufficient. Any idea how performant it is? – fishycrakers Sep 30 '19 at 12:21
  • 1
    @fishycrakers For the future, please mind that formally your question almost fully falls under one of the definitions of offtopic on SO: "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." I'm not marking it since you have specified a (pseudo)code snippet and explicitly mentioned what have you tried. – Sergey Kudriavtsev Sep 30 '19 at 12:23
  • Well, compared to compiled code it's awfully slow, of course. But there are tweaks, so you can save "precompiled" scripts and keep them in memory and such stuff. But I'd start tweaking performance only if and when it becomes an issue. – Fildor Sep 30 '19 at 12:23
  • 1
    If it is what you are looking for, feel free to write up an example as an answer yourself. I wouldn't stand up to my own standards just answering "Use this"... – Fildor Sep 30 '19 at 12:25

0 Answers0