As with any code .... rarely is the "best" way. There are several ways to do things, and each of them has advantages and disadvantages.
However, some form of template interpreter is probably the most common way. According to the GoF book :
The interpreter pattern is widely used in compilers implemented using object-oriented languages, since the Smalltalk Compilers. SPECTalk uses a template to interpret the description of input file formats. QOCA Restriction Toolkit uses it to evaluate constraints.
It also refers to limitations in the applicability section.
- grammar is simple. For complex grammars, the class hierarchy for grammars becomes large and unmanageable. Tools, such as parser generators, are the best alternative in such cases
- Efficiency is not a serious problem. Most Effective Translators are usually not implemented by interpreting parsing trees, but first translating them into another form. For example, regular expressions are often transformed into state machines. But even then, the translator may be a template implemented by the translator, so the template is still applicable.
Understanding this, you should now know why it is better to precompile your reusable RegEx before doing many matches with it. If you do not, each time you will have to perform both actions (conversion, interpretation), and not create a state machine once and apply it several times.
In particular, for the type of interpretation you are describing, Microsoft provides the Microsoft.VisualStudio namespace and all its powerful features as part of the Visual Studio SDK . You can also look at System.CodeDOM for generating and compiling dynamic code.
source share