I made a system that creates a simple string with Function/Response format, example:
Check('Value'):ShowImage(@)|Check('Value'):OtherFunction(@)....and so on
Where Check is the name of a function, Value is the parameter, ShowImage is the name of a Response function, @ is the entry paremeter (result of the previous function). The pipe splits another Function/Response pair that fires if the first Check('Value') function once “checked” were not satisfied (say, if the parameter was not accomplished the Check condition the function is invalid and hence the Response part in the first Function/Response pair is not executed, so system keep trying Functions awaiting to find the one that executes the right Response).
The way the application should work is to evaluate each rule (similar to a JavaScript eval function) and take appropriate action based on function results.
At first glance, it looks complicated, because first of all I need to cast the string to the right real C# function that will actually process the condition. Therefore, depending on the function result, decide where to point to execute my Response function.
Furthermore: This is just the kind example, because there are functions as * that represent something like: “any condition is true” what in almost all cases this function is the last in the chain (the default function).
That’s my problem, I can’t realize what is the easiest way to cope with this problem.
Maybe a chain of delegates? Lambdas? Anonymous stored into a structure…
Could you give me your measure/advise? Where to start?
Depending on the level of extensibility you want to have, I would say the most extensible way would be to use reflection to get method references, after you have parsed the input string.
You can start by splitting your problem into smaller subproblems.
Let’s say you are aiming for something like this:
A “result” as it’s used above would then have an interface like this:
And, if you want to delegate actual actions to some unknown methods, a reasonable way to implement it would be something like this:
What’s left is to use some reflection to get the actual methods out of your strings:
That is, more or less, your program’s skeleton. You should be able to fill in the missing parts yourself, as an exercise. If you have problems, I can update the answer later.
A
GetMethodAndParamsmethod should split the input string into a Tuple (or your custom class) which contains the method name and its params as plain strings.TesterandExecutorclasses can also be implemented trivially.